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

Create new multiple folders using string path? 4

Status
Not open for further replies.

Oliver2003

Technical User
Apr 19, 2003
144
GB
Is it possible to create multiple new folders using a single path,
e.g.

StrNewFolders = "C:\Folder1\Folder2\Folder2" etc

non of the folders exists and I would like to create them using the string above,

I have tried using the mkdir function but can only get this to create one folder at a time e.g. c:\Folder1

??
 
AFAIK, you have to build them one at a time from the top on down.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I have found the following code by Andy Watt in thread 181-540942

Public Sub MakeFolder(strPath As String)
Dim fso As New Scripting.FileSystemObject
Dim str As String

If Not fso.FolderExists(strPath) Then
str = fso.GetParentFolderName(strPath)
If str <> vbNullString Then MakeFolder str
fso.CreateFolder strPath
End If
End Sub

but I am getting the error User defined type not defined - the code highlighted is fso As New Scripting.FileSystemObject

does a reference need to be added to use this code?

Thank you
 
Hi,

Very interesting, the code should read:

Public Sub MakeFolder(strPath As String)
Dim fso, str As String
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
If Not fso.FolderExists(strPath) Then
str = fso.GetParentFolderName(strPath)
If str <> vbNullString Then MakeFolder str
fso.CreateFolder strPath
End If
End Sub

Take note of CajunCenturion's excellent advice.

Bill
 
After a lot more searching, I found just what I wanted in thread 222-265528 posted by Justin Ezequiel,

you just pass the full path of all the folders you need creating :

Call BuildPath(&quot;C:\Folder1\Folder2\Folder3&quot;)


Private Function BuildPath(ByVal sPath As String) As Boolean
Dim oFSO
Dim oDrive
Dim sDrive As String
Dim sParent As String

On Error GoTo ERRHANDLER

Set oFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
If oFSO.FileExists(sPath) Then
MsgBox &quot;File '&quot; & sPath & &quot;' exists&quot;
Else
sDrive = oFSO.GetDriveName(sPath)
If oFSO.DriveExists(sDrive) Then
Set oDrive = oFSO.GetDrive(sDrive)
If oDrive.IsReady Then
If Not oFSO.FolderExists(sPath) Then
sParent = oFSO.GetParentFolderName(sPath)
If oFSO.FolderExists(sParent) Then
oFSO.CreateFolder sPath
BuildPath = True
Else
If BuildPath(sParent) Then
oFSO.CreateFolder sPath
BuildPath = True
End If
End If
Else
BuildPath = True
End If
Else
MsgBox &quot;Drive '&quot; & sDrive & &quot;' is not ready&quot;
End If
Set oDrive = Nothing
Else
MsgBox &quot;Drive '&quot; & sDrive & &quot;' does not exist&quot;
End If
End If
Set oFSO = Nothing

Exit Function

ERRHANDLER:
MsgBox &quot;Building path '&quot; & sPath & &quot;'&quot; & vbCrLf & _
Err.Description
End Function

 
Ho Oliver2003,

Just shows you can't believe everything you're told. Well done.

Also quadruple well done for not plagiarizing Justin's post.

A star for both you and Justin. My partner MK10 will be on tomorrow, there'll be another star for you both.

Thanks a million.

Bill

 
You need to set a reference to Microsoft Scripting Runtime to get access to the File System Object (scrrun.dll). It is a very powerful object. It exposes a Drives collection and a Files and Folders collection for each RootFolder. This makes it very easy to iterate a drive and subfolders using a recursive function call.

Good Luck!

PS. One of my pet peeves with help is that they will give a great example of something but when you try it it won't work because you need a reference set and they never tell you which one it is.

 
Both of the solutions presented do in fact, build the tree, one directory at a time, from the top down. It may not appear that the directories are being created one at a time, because the recursive processes obscure that fact, but only one directory is being created at a time, one in each invocation of the either the MakeFolder or BuildPath functions as written by Andy and Justin respectively.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top