Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create UNC Directory Tree

Status
Not open for further replies.

DJX995

IS-IT--Management
Jan 12, 2009
20
US
I'm tring to use this function, that I found here:

Sub CreateFolderTree(strTempPath)
On Error Resume Next
If strTempPath <> "" Then
If Not objFSO.FolderExists(objFSO.GetParentFolderName(strTempPath)) Then
Call CreateFolderTree(objFSO.GetParentFolderName(strTempPath))
Else
objFSO.CreateFolder(strTempPath)
End If
Else
' Lowest Directory
End If
On Error GoTo 0
End Sub

I call it like this:
Call CreateFolderTree(strDestinationRoot & strSourceApp)

The vars look something like this:
strDestinationRoot = "\\UNCServer.tld\Directory\" & strUserName & "\Something\Something"
strSourceApp = "\Folder\From Root"

Unfortunatly, I have to run it twice inorder for it to create the directory tree in my example.

Can any one lend some help?

 
the code looks fine. My guess is that an error occurs that first time through that doesn't on the second time. This code creates a directory recursively. If it works for you, look for the differences in code. You might also want to REM on error resume next, so you can see where the script fails.

function createDirectory (strDir)
if (objFSO.FolderExists(strDir) = false) then
strFile = right(strDir, len(strDir) - inStrRev(strDir, "\"))
strParentDir = left(strDir, len(strDir) - len(strFile) - 1)
if NOT (objFSO.FolderExists(strParentDir)) then createDirectory (strParentDir)
if NOT (objFSO.FolderExists(strDir)) then objFSO.CreateFolder (strDir)
end if
end function

-Geates
 
ok, the problem is that

Code:
If Not objFSO.FolderExists(objFSO.GetParentFolderName(strTempPath)) Then
   CreateFolderTree(objFSO.GetParentFolderName(strTempPath))
Else
   objFSO.CreateFolder(strTempPath)
End If

should be

Code:
If Not objFSO.FolderExists(objFSO.GetParentFolderName(strTempPath)) Then CreateFolderTree(objFSO.GetParentFolderName(strTempPath))
If Not objFSO.FolderExists(strTempPath) then objFSO.CreateFolder(strTempPath)

the reason why is because you still need to create the child directory after you create the parent. Because you have an If..Else, either the child or parent folder gets created, but not both. Your code simple ends after the parent is created. Interestingly, your code needs to be run once for every child folder. Meaning, your code needs to be run 3 times to create c:\three\folders\deep.

-Geates



[/code]
 
Great, thanks man.
I think we now have the smallest way to do this.
Here's what it looks like all together:

Sub CreateFolderTree(strTempPath)
If Not objFSO.FolderExists(objFSO.GetParentFolderName(strTempPath)) Then
CreateFolderTree(objFSO.GetParentFolderName(strTempPath))
End If

If Not objFSO.FolderExists(strTempPath) Then
objFSO.CreateFolder(strTempPath)
End If
End Sub

 
wait! even smaller (not really, just compact).

Sub CreateFolderTree(strTempPath)
If Not objFSO.FolderExists(objFSO.GetParentFolderName(strTempPath)) Then CreateFolderTree(objFSO.GetParentFolderName(strTempPath))
If Not objFSO.FolderExists(strTempPath) Then objFSO.CreateFolder(strTempPath)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top