Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different commands, is there an easy way similar to Directory.CreateDirectory()
From stackoverflow
-
Here's some code I used in one of my projects. It requires a reference be added to the project for the file system object.
First, click Project -> References, scroll down to "Microsoft Scripting Runtime" and select it. Then you can use this function:
Public Sub MakePath(ByVal Folder As String) Dim arTemp() As String Dim i As Long Dim FSO As Scripting.FileSystemObject Dim cFolder As String Set FSO = New Scripting.FileSystemObject arTemp = Split(Folder, "\") For i = LBound(arTemp) To UBound(arTemp) cFolder = cFolder & arTemp(i) & "\" If Not FSO.FolderExists(cFolder) Then Call FSO.CreateFolder(cFolder) End If Next End Sub
-
As an alternative, here is a function I wrote that takes a complete path including a drive letter if needed as a parameter. It then walks the path and traps the VB error number 76 (path not found). When the error handler traps an error 76 it creates the folder that caused the error and resumes walking the path.
Public Function Check_Path(rsPath As String) As Boolean Dim dPath As String Dim i As Integer Dim sProductName As String On Error GoTo Check_Path_Error If Left$(UCase$(rsPath), 2) Left$(UCase$(CurDir), 2) Then ChDrive Left$(rsPath, 2) End If i = 3 Do While InStr(i + 1, rsPath, "\") > 0 dPath = Left$(rsPath, InStr(i + 1, rsPath, "\") - 1) i = InStr(i + 1, rsPath, "\") ChDir dPath Loop dPath = rsPath ChDir dPath Check_Path = True Exit Function Check_Path_Error: If Err.Number = 76 Then 'path not found' MkDir dPath 'create the folder' Resume Else sProductName = IIf(Len(App.ProductName) = 0, App.EXEName, App.ProductName) MsgBox "There was an unexpected error while verifying/creating directories." _ & vbCrLf & vbCrLf & "Error: " & CStr(Err.Number) & ", " & Err.Description & ".", _ vbOKOnly + vbCritical, sProductName & " - Error Creating File" Check_Path = False End If End Function
0 comments:
Post a Comment