We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

vbscript create a subfolder

rrangel8
rrangel8 asked
on
Medium Priority
628 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.
Comment
Watch Question

Commented:
In code2, does the Test1 folder exist?

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

Ss

Author

Commented:
Let me put it this way. The Test 1 folder exists. I am trying to get it to create the test2 folder.
HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
CreateFolder
Creates all directories and subdirectories as specified by path

so issue is not Test1 exists or not...

Author

Commented:
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"
HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
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

HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
or did you try

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

Author

Commented:
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.

Author

Commented:
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"
HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
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)
Sr. System Analyst
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
thx
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.