Creating multiple folder levels

  • Thread starter Thread starter SiriS
  • Start date Start date
S

SiriS

Hello,
I would like to create a folder path that a user has given. If th
parent folder exists, there is no problem, but if it does not, the pat
must be created.

If the user specifies a folder path like C:\Folder1\Folder2\Folder3\
and either of Folder1, Folder2 and Folder3 exists they all must b
created.

I use the filesystemobject. I would like to do something like this:

sub createPath (myPath as string)

Set fs = CreateObject("Scripting.FileSystemObject")

fs.createfolder (myPath)

Select Case Err.Number
Case 0: ' OK, directory is created, do nothing
Case 58: 'OK, directory exists already
Err.Clear
Case 76 ' Path not found - parent folders must be created!

XXXXX

end select

end sub

Thank you!

Sir
 
if using excel 2000 or later, you don't need to go outside VBA to do this.

Sub Tester2()
Dim fFolder As Variant
Dim sStr As String, sStr1 As String
sStr = "C:\Folder1\Folder2\Folder3\"
If Right(sStr, 1) = "\" Then
sStr = Left(sStr, Len(sStr) - 1)
End If
vFolder = Split(sStr, "\")
On Error Resume Next
sStr1 = vFolder(LBound(vFolder))
For i = LBound(vFolder) + 1 To UBound(vFolder)
sStr1 = sStr1 & "\" & vFolder(i)
MkDir sStr1
Next
On Error GoTo 0

End Sub
 
Back
Top