与VB6中的Directory.CreateDirectory()等效

与VB6中的Directory.CreateDirectory()等效,vb6,Vb6,尝试一次创建多个文件夹层C:\pie\applepie\recipies\ 如果不使用几个不同的命令,有没有一种简单的方法类似于Directory.CreateDirectory()以下是我在一个项目中使用的一些代码。它要求将文件系统对象的引用添加到项目中 首先,单击Project->References,向下滚动到“Microsoft脚本运行时”并选择它。然后您可以使用此功能: Public Sub MakePath(ByVal Folder As String) Dim arTem

尝试一次创建多个文件夹层C:\pie\applepie\recipies\
如果不使用几个不同的命令,有没有一种简单的方法类似于Directory.CreateDirectory()

以下是我在一个项目中使用的一些代码。它要求将文件系统对象的引用添加到项目中

首先,单击Project->References,向下滚动到“Microsoft脚本运行时”并选择它。然后您可以使用此功能:

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

作为替代方案,这里是我编写的一个函数,它采用一个完整的路径,如果需要,还包括一个驱动器号作为参数。然后它遍历路径并捕获VB错误号76(未找到路径)。当错误处理程序捕获错误时,它将创建导致错误的文件夹并继续遍历路径

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 公共函数Check_Path(rsPath为字符串)为布尔值 将dPath设置为字符串 作为整数的Dim i Dim sProductName作为字符串 On Error GoTo检查路径错误 如果左$(UCase$(rsPath),2)左$(UCase$(CurDir),2)则 ChDrive Left$(rsPath,2) 如果结束 i=3 安装时执行(i+1,rsPath,“\”)>0 dPath=Left$(rsPath,InStr(i+1,rsPath,“\”)-1) i=InStr(i+1,rsPath,“\”) ChDir-dPath 环 dPath=rsPath ChDir-dPath 检查路径=真 退出功能 检查路径错误: 如果Err.Number=76,则“未找到路径” MkDir dPath“创建文件夹” 简历 其他的 sProductName=IIf(Len(App.ProductName)=0,App.EXEName,App.ProductName) MsgBox“验证/创建目录时出现意外错误。”_ &vbCrLf&vbCrLf&“错误:”&CStr(错误编号)&“,”&Err.Description&“_ vbOKOnly+vbCritical,sProductName&-创建文件时出错 检查路径=False 如果结束 端函数
'而无需引用FileSystemObject

Public Sub MkPath(ByVal sPath As String)
  Dim Splits() As String, CurFolder As String
  Dim i As Long
  Splits = Split(sPath, "\")
  For i = LBound(Splits) To UBound(Splits)
    CurFolder = CurFolder & Splits(i) & "\"
    If Dir(CurFolder, vbDirectory) = "" Then MkDir CurFolder
  Next i
End Sub

然而,另一个简单的方法是:

Public Sub MakePath(ByVal Path As String)
    On Error Resume Next
    Shell "cmd /c mkdir """ & Path & """"
End Sub