Link to home
Start Free TrialLog in
Avatar of rrangel8
rrangel8

asked on

vbscript create a subfolder

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.
Avatar of sshah254
sshah254

In code2, does the Test1 folder exist?

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

Ss
Avatar of rrangel8

ASKER

Let me put it this way. The Test 1 folder exists. I am trying to get it to create the test2 folder.
CreateFolder
Creates all directories and subdirectories as specified by path

so issue is not Test1 exists or not...
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"
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

or did you try

If Not objFSO.folderexists(Struserprofile & "\Application Data\Test1")
-->
If Not objFSO.folderexists(Struserprofile & "\Application Data\Test1\")
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.
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"
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)
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thx