I’m working on a script that basically copies folders and files from a server to local computers. While working on this, I’ve found that I’m in need of a function of some sort that basically takes a string of a full folder path, splits it up and checks each folder if it exists. If it doesn’t, create the folder.
So, I was thinking of a smart way of doing this so I can reuse the code later.
I would like it to take one argument, the string of the full path. And the code does the rest.
Edit: This is the completed code from Jean-François Corbett. I’ll give you over 9000 thank you’s!
Public Sub createFolderStructure(ByVal strFullPath)
Set objFSO = CreateObject("Scripting.FileSystemObject")
' How many levels are there?
nLevel = 0
strParentPath = strFullPath
Do Until strParentPath = ""
strParentPath = objFSO.GetParentFolderName(strParentPath)
nLevel = nLevel + 1
Loop
For iLevel = 1 To nLevel
' Figure out path for directory at level iLevel
strParentPath = strFullPath
For j = 1 To nLevel - iLevel
strParentPath = objFSO.GetParentFolderName(strParentPath)
Next
' Does this directory exist? If not, create it.
If objFSO.FolderExists(strParentPath) = False Then
Set newFolder = objFSO.CreateFolder(strParentPath)
End If
Next
End Sub
Since you’ve got
objFSOanyway, why not use itsGetParentFolderNameandFolderExistsmethods. Really,FileSystemObjecthas most of this figured out already, so spare yourself some pain; no need to reinvent the wheel.EDIT: Here’s an example of what I think you want to do. Adapt as you see fit.