PDA

View Full Version : Solved: MkDir, multiple directories?



Ago
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.
D:\Testfolder

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:
D:\Testfolder\Test1
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?

Bob Phillips
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

Ago
04-02-2010, 07:02 AM
It worked!
Sweet!
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