rrangel8
asked on
vbscript create a subfolder
This code works:
If Not objFSO.folderexists(strAll Profiles & "\Application Data\Test1") Then
objFSO.CreateFolder(strAll Profiles & "\Application Data\Test1")
This code doesn't:
If Not objFSO.folderexists(strAll Profiles & "\Application Data\Test1\Test2") Then
objFSO.CreateFolder(strAll Profiles & "\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.
If Not objFSO.folderexists(strAll
objFSO.CreateFolder(strAll
This code doesn't:
If Not objFSO.folderexists(strAll
objFSO.CreateFolder(strAll
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.
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...
Creates all directories and subdirectories as specified by path
so issue is not Test1 exists or not...
ASKER
My thinking is if test2 doesn't exist then it will create it.
If Not objFSO.folderexists(Struse rprofile & "\Application Data\Test1\Test2") Then
objFSO.CreateFolder(struse rprofile & "\Application Data\Test1\Test2")
This works fine below:
'Create Objects
Set objShell = CreateObject("WScript.Shel l")
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
'Get special folder paths
strAllUserProfile = objShell.ExpandEnvironment Strings("% USERPROFIL E%")
'Get Path of profile folder
strAllProfiles = objFSO.GetParentFolderName (strAllUse rProfile)
Set objProfiles = objFSO.GetFolder(strAllPro files)
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
'Exclude System Default Profiles
Set SkipProfilesArray = CreateObject("Scripting.Di ctionary")
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(s trUserProf ile.Name) Then
If Not objFSO.folderexists(Struse rprofile & "\Application Data\Test1") Then
objFSO.CreateFolder(struse rprofile & "\Application Data\Test1")
WSCRIPT.ECHO "1"
End If
Else
End If
Next
WScript.Echo vbCrLf & "Script Finished"
If Not objFSO.folderexists(Struse
objFSO.CreateFolder(struse
This works fine below:
'Create Objects
Set objShell = CreateObject("WScript.Shel
Set objFSO = CreateObject("Scripting.Fi
'Get special folder paths
strAllUserProfile = objShell.ExpandEnvironment
'Get Path of profile folder
strAllProfiles = objFSO.GetParentFolderName
Set objProfiles = objFSO.GetFolder(strAllPro
Set objFSO = CreateObject("Scripting.Fi
'Exclude System Default Profiles
Set SkipProfilesArray = CreateObject("Scripting.Di
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(s
If Not objFSO.folderexists(Struse
objFSO.CreateFolder(struse
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)
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
or did you try
If Not objFSO.folderexists(Struse rprofile & "\Application Data\Test1")
-->
If Not objFSO.folderexists(Struse rprofile & "\Application Data\Test1\")
If Not objFSO.folderexists(Struse
-->
If Not objFSO.folderexists(Struse
ASKER
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.
ASKER
ok. I am an idiot....i change the logic a little. Removed the "if not"
If objFSO.folderexists(Struse rprofile & "\Application Data\Vandyke") Then
objFSO.CreateFolder(struse rprofile & "\Application Data\Vandyke\SecureCRT")
WSCRIPT.ECHO "1"
If objFSO.folderexists(Struse
objFSO.CreateFolder(struse
WSCRIPT.ECHO "1"
yes, you should check all dirs until root, if it does not exists create it
if objFSO.folderexists("c:\da ta\dir1") then objFSO.CreateFolder("c:\da ta\dir1")
if objFSO.folderexists("c:\da ta\dir1\di r2") then objFSO.CreateFolder("c:\da ta\dir1\di r2")
if objFSO.folderexists("c:\da ta\dir1\di r2\dir3") then objFSO.CreateFolder("c:\da ta\dir1\di r2\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)
if objFSO.folderexists("c:\da
if objFSO.folderexists("c:\da
if objFSO.folderexists("c:\da
... 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thx
If not, you will need to create Test1 first, and then create Test2
Ss