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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create multiple subfolders at one time 3

Status
Not open for further replies.

Griffinator

Programmer
Dec 30, 2002
28
0
0
CA
Hi,

I am having trouble creating multiple subfolders at one time using the File System Object.

Example:

Using the CreateFolder() method of the FileSystemObject, I am having no trouble creating the folder "Backup" in say the root of the drive (ie: "C:\Backup"). This holds true for any path that exists up to "\Backup"

However, if I try to create the path "C:\Backup\Data" when neither the Backup folder nor the Data folder currently exist, I get a "Path not found" error. I understand that this is happening because the Data folder cannot be created in a folder that doesn't currently exist, but I am unable to figure out a way to first create the Backup folder then the Data folder so that this error doesn't occur.

The reason I am doing this is because the path is user defined, so I will never be sure how many folders and subfolders the user will want to create.

Thanks in advance for any and all suggestions.
 
this is the only way:
FSO.CreateFolder("C:\backup")
FSO.CreateFolder("C:\backup\subfolder")

Known is handfull, Unknown is worldfull
 
Thanks for your suggestion, vbkris.

However, the problem that I am trying to figure out is how to know where the existing folders in a path end, and where the new folders in the path begin.

For the following path: "C:\Documents And Settings\Griffin\Desktop\Backup\Data", "C:\Documents And Settings\Griffin\Desktop" is an existing path in which I could create the folder Backup. From there, I could then create the folder Data.

Basically, in any path, I need to know where to start creating folders that don't already exist.

I appreciate your feedback, any examples you could give me would be greatly appreciated.
 
i think there is a command for this:
fso.FolderExist(path)

i am still checking...

Known is handfull, Unknown is worldfull
 
vbkris,

Yes, there is a FolderExists() function for the FileSystemObject which I am currently using to determine if the path the user has specified currently exists. If it doesn't, I need to be able to create the necessary subfolders that don't exist. This works fine using the CreateFolder() method provided that the user has only specified 1 subfolder in the path that doesn't exist (ie: "C:\Documents And Settings\Griffin\Desktop\Backup).

However, the FolderExists() method doesn't tell me if the folder named Backup exists in the path "C:\Documents And Settings\Griffin\Desktop\Backup\Data".

Do you know of a way to work through the path, 1 folder at a time, to determine if each folder exists? ie: Start with C:\ and check if it exists, then move to C:\Document and Settings and check if it exists, etc....

If I can determine where the existing folders end, I can then begin creating the new folders as you have suggested.

Thanks in advance
 
Sadly, the FSO doesn't support this functionality natively - althugh you can leverage it's ability to walk up the path to create a fairly useful recursive function that does this, eg:
[tt]
Private Function vbFolderCreatePath(ByVal strPath As String, Optional ByVal AllowCancel As Boolean = False) As Long
Dim fso As FileSystemObject
Static CancelCreate
Dim result As Long

CancelCreate = False
If AllowCancel Then
result = MsgBox(strPath & vbCrLf & vbCrLf & "does not exist. Do you want to create it?", vbYesNo)
If result = vbNo Then CancelCreate = True
End If

With New FileSystemObject
Do Until .FolderExists(strPath) Or CancelCreate
If .FolderExists(.GetParentFolderName(strPath)) Then
.CreateFolder strPath
Else
vbFolderCreatePath .GetParentFolderName(strPath), AllowCancel
End If
Loop
End With

End Function
 
Thank you strongm!!!

That works beautifully! A star for you.

This place is awesome. You have no idea how many times this forum has pulled my behind out of the fire!
 
For interests sake, I am posting my slightly modified version of strongm's previous post. The one minor glitch that I found was that it was prompting me to create each sub folder on each recursive call. The following code corrected this:

Private Function ff_CreateSpecifiedPath(ByVal strPath As String, _
Optional ByVal blnPromptToCreate As Boolean = False) As Boolean

'*************************************************************************************
'**PURPOSE: To create the path specified by the user in the BackupFileName textbox
'**
'**ACCEPTS: 1) A string variable, which defines the path specified by the user
'** 2) A boolean variable, which indicates whether or not to prompt the
'** user to create the specified path
'**
'**RETURNS: A boolean value indicating whether or not the path was successfully created
'*************************************************************************************

On Error GoTo ErrCreatePath:

If blnPromptToCreate Then
'the PromptToCreate flag is set to true. Prompt the user to create the specified path

intUserResponse = MsgBox("The folder '" & strPath & "' does not exist!" & vbCrLf & _
"Do you wish to create it now?", vbYesNo, "Create New Folder?")

If intUserResponse = vbNo Then
'the user doesn't wish to create the specified path.

'return a value of False to the calling sub to indicate the path was not
'created
ff_CreateSpecifiedPath = False

Exit Function

End If

End If

With New FileSystemObject

'loop through the specified path until an existing folder is found
Do Until .FolderExists(strPath)

If .FolderExists(.GetParentFolderName(strPath)) Then
'the parent folder in the path currently exists. Create the specified folder

.CreateFolder strPath

Else
'the parent folder doesn't exist

'recursively call this function with the next parent folder to see if
'the next parent folder exists
ff_CreateSpecifiedPath .GetParentFolderName(strPath), False

End If

Loop

End With

'return a value of True to the calling sub to indicate that the path was successfully
'created
ff_CreateSpecifiedPath = True

Exit Function

ErrCreatePath:

'an error has occurred creating the specified path. Warn the user
MsgBox "The following error occurred while processing the requested task:" _
& vbCrLf & "Error Number: " & Err.Number & vbCrLf & "Error Description: " _
& Err.Description, vbOKOnly + vbExclamation, "Error While Processing Task"

'return a value of false to the calling sub to indicate that the path was not
'successfully created
ff_CreateSpecifiedPath = False

End Function


My naming conventions are slightly different as well. The function returns a boolean value to indicate whether or not to proceed with the file copy of the selected backup, and intUserResponse is a form level variable used throughout the module.

Just thought I'd post this back in case anyone was interested.
 
Splendid stuff. Most of the examples I give here on tek-tips can be considered starting points rather than complete solutions. It's nice to see someone not only taking up that challenge, but coming back to illustrate their changes for other people to see. For that I give you a star.
 
What Reference do you need to run FileSystemObject code ?

I'm getting an error...

tia !

--------------------------------------
"Hacker by Heart"
saenzcorp@hotpop.com
 
OK, it appears to by "Microsoft Scripting Runtime"

if anyone know different please comment...

tia

--------------------------------------
"Hacker by Heart"
saenzcorp@hotpop.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top