?
Solved

vbscript create a subfolder

Posted on 2011-04-29
11
Medium Priority
?
616 Views
Last Modified: 2012-06-27
This code works:
          If Not objFSO.folderexists(strAllProfiles & "\Application Data\Test1") Then
          objFSO.CreateFolder(strAllProfiles & "\Application Data\Test1")

This code doesn't:
          If Not objFSO.folderexists(strAllProfiles & "\Application Data\Test1\Test2") Then
          objFSO.CreateFolder(strAllProfiles & "\Application Data\Test1\Test2")

Can't get it to create the test2 folder. Cant get past that 1st level. It will create the Test1 folder but gives me "path cannot be found" if i try to create the Test2 folder.

Any help or pointers.
0
Comment
Question by:rrangel8
  • 5
  • 5
11 Comments
 
LVL 9

Expert Comment

by:sshah254
ID: 35491774
In code2, does the Test1 folder exist?

If not, you will need to create Test1 first, and then create Test2

Ss
0
 

Author Comment

by:rrangel8
ID: 35491866
Let me put it this way. The Test 1 folder exists. I am trying to get it to create the test2 folder.
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 35491939
CreateFolder
Creates all directories and subdirectories as specified by path

so issue is not Test1 exists or not...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rrangel8
ID: 35492035
My thinking is if test2 doesn't exist then it will create it.
          If Not objFSO.folderexists(Struserprofile & "\Application Data\Test1\Test2") Then
          objFSO.CreateFolder(struserprofile & "\Application Data\Test1\Test2")

This works fine below:

'Create Objects
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Get special folder paths
strAllUserProfile = objShell.ExpandEnvironmentStrings("%USERPROFILE%")

'Get Path of profile folder
strAllProfiles = objFSO.GetParentFolderName(strAllUserProfile)
Set objProfiles = objFSO.GetFolder(strAllProfiles)
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Exclude System Default Profiles
Set SkipProfilesArray = CreateObject("Scripting.Dictionary")
SkipProfilesArray.Add "BLAH.BLAH", "altirisadmin.ercot"
SkipProfilesArray.Add "All Users", "All Users"
SkipProfilesArray.Add "Default User", "Default User"
SkipProfilesArray.Add "BLEEP", "Ozark"
SkipProfilesArray.Add "BLEEP.BLEEP", "BLEEP.ERCOT"

'Find file
For Each strUserProfile In objProfiles.SubFolders
    If Not SkipProfilesArray.Exists(strUserProfile.Name) Then
                   
          If Not objFSO.folderexists(Struserprofile & "\Application Data\Test1") Then
          objFSO.CreateFolder(struserprofile & "\Application Data\Test1")
          WSCRIPT.ECHO "1"


     
    End If
    Else
    End If
    Next
   WScript.Echo vbCrLf & "Script Finished"
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 35492176
my bad, that explanation is vb code ;)

here is the vba code to make full pathy (tested)
Sub CreateSubDirectories(fullPath)

Dim str
Dim strArray
Dim i
Dim basePath
Dim newPath

Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")

str = fullPath

' add trailing slash
If Right(str, 1) <> "\" Then
 str = str & "\"
End If

' split string into array
strArray = Split(str, "\")

basePath = strArray(0) & "\"

' loop through array and create progressively
' lower level folders
For i = 1 To UBound(strArray) - 1
  If Len(newPath) = 0 Then
    newPath = basePath & newPath & strArray(i) & "\"
  Else
    newPath = newPath & strArray(i) & "\"
  End If

  If Not fso.folderexists(newpath) Then 
  	WScript.Echo newpath
    fso.CreateFolder(newPath)
  End If
Next

End Sub
    
Dim newpath
newpath = "c:\temp\Application\Data\Test1\Test2"
CreateSubDirectories newpath

Open in new window

0
 
LVL 61

Expert Comment

by:HainKurt
ID: 35492193
or did you try

If Not objFSO.folderexists(Struserprofile & "\Application Data\Test1")
-->
If Not objFSO.folderexists(Struserprofile & "\Application Data\Test1\")
0
 

Author Comment

by:rrangel8
ID: 35492633
thx HainKurt...but that seems like a lot of code just to create a folder a couple of levels down. Your code does work and i am trying to integrate the logic into mine but to a newbie....it may be a little to difficult.
0
 

Author Comment

by:rrangel8
ID: 35492676
ok. I am an idiot....i change the logic a little. Removed the "if not"

                   
      If objFSO.folderexists(Struserprofile & "\Application Data\Vandyke") Then
          objFSO.CreateFolder(struserprofile & "\Application Data\Vandyke\SecureCRT")
          WSCRIPT.ECHO "1"
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 35492845
yes, you should check all dirs until root, if it does not exists create it

if objFSO.folderexists("c:\data\dir1") then objFSO.CreateFolder("c:\data\dir1")
if objFSO.folderexists("c:\data\dir1\dir2") then objFSO.CreateFolder("c:\data\dir1\dir2")
if objFSO.folderexists("c:\data\dir1\dir2\dir3") then objFSO.CreateFolder("c:\data\dir1\dir2\dir3")
... and so on...

or just use the function I posted

CreateSubDirectories "c:\data\dir1\dir2\dir3"

it will check all folders and create them one by one :) just one line (after adding the function)
0
 
LVL 61

Accepted Solution

by:
HainKurt earned 1000 total points
ID: 35492852
oops I forgot nots :)

above should read as this:

if not objFSO.folderexists("c:\data\dir1") then objFSO.CreateFolder("c:\data\dir1")
if not objFSO.folderexists("c:\data\dir1\dir2") then objFSO.CreateFolder("c:\data\dir1\dir2")
if not objFSO.folderexists("c:\data\dir1\dir2\dir3") then objFSO.CreateFolder("c:\data\dir1\dir2\dir3")

... and so on...

or just use the function I posted

CreateSubDirectories "c:\data\dir1\dir2\dir3"

it will check all folders and create them one by one :) just one line (after adding the function)
0
 

Author Closing Comment

by:rrangel8
ID: 35494210
thx
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question