View Full Version : Solved: MkDir, multiple directories?

04-02-2010, 05:41 AM
I just noticed a problem in my code.
The user can choose a folder with a Filedialog and the path will be printed in TextBox3.

The user should now be able to make a new directory in this textbox.

An easy example, user selects Drive D which is completly empty.
Textbox shows D:\
The user can now type a new directoryname in the textbox.

If Dir(UserForm2.TextBox3.Value, vbDirectory) = "" Then 'If folder dont exist
MkDir UserForm2.TextBox3.Value 'Create it
End If
And the folder is created, but if user types:
The code will stop because Testfolder has not been created yet.

Is there any way i can create multiple direcrories?
I know i could "loop" trough each part of the path, but is there any easier ways?

04-02-2010, 06:55 AM
Add this procedure

Private Sub MakeDirectory(dir As String)
Dim aryDir As Variant
Dim dirName As String
Dim i As Long

On Error Resume Next
aryDir = Split(dir, "\")
dirName = aryDir(LBound(aryDir))
For i = LBound(aryDir) + 1 To UBound(aryDir)

dirName = dirName & "\" & aryDir(i)
MkDir dirName
Next i
End Sub

then change your code to

If Dir(UserForm2.TextBox3.Value, vbDirectory) = "" Then 'If folder dont exist
MakeDirectory UserForm2.TextBox3.Value 'Create it
End If

04-02-2010, 07:02 AM
It worked!
Thank you!

Kenneth Hobs
04-02-2010, 07:21 AM
If dir(UserForm2.TextBox3.Value, vbDirectory) = "" Then 'If folder dont exist
Shell "cmd /c mkdir """ & UserForm2.TextBox3.Value & """ 'Create it"
End If