首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于创建目录和子目录的FileSystemObject.CreateFolder

用于创建目录和子目录的FileSystemObject.CreateFolder
EN

Stack Overflow用户
提问于 2015-06-25 02:04:31
回答 3查看 16.8K关注 0票数 9

我想用以下代码创建一个目录和子目录:

代码语言:javascript
运行
复制
Public fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
fso.CreateFolder ("C:\Users\<my_username>\DataEntry\logs")

我正在尝试创建嵌套目录。在本例中,DataEntry目录将不存在,因此基本上我希望在C:\Users\<username>下创建两个目录:DataEntry\logs

如果我进入命令提示符,我可以使用mkdir创建目录,而不会出现任何问题。但是,我根本无法让VBA创建该文件夹,我得到了:

代码语言:javascript
运行
复制
Run-time error '76':

Path not found                        

我正在使用Excel VBA 2007/2010

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-06-25 02:27:30

需要创建每个文件夹一次一个。您可以使用下面这样的代码来完成此操作:

代码语言:javascript
运行
复制
Sub tgr()

    Dim strFolderPath As String
    Dim strBuildPath As String
    Dim varFolder As Variant

    strFolderPath = "C:\Users\<my_username>\DataEntry\logs"

    If Right(strFolderPath, 1) = "\" Then strFolderPath = Left(strFolderPath, Len(strFolderPath) - 1)
    For Each varFolder In Split(strFolderPath, "\")
        If Len(strBuildPath) = 0 Then
            strBuildPath = varFolder & "\"
        Else
            strBuildPath = strBuildPath & varFolder & "\"
        End If
        If Len(Dir(strBuildPath, vbDirectory)) = 0 Then MkDir strBuildPath
    Next varFolder

    'The full folder path has been created regardless of nested subdirectories
    'Continue with your code here

End Sub
票数 7
EN

Stack Overflow用户

发布于 2018-06-12 21:10:12

泰格阿凡达的循环答案可能行得通,但读起来有点难。与自己对字符串进行微观管理不同,FileSystemObject提供了路径操作函数,而且递归比循环更容易阅读。

下面是我使用的函数:

代码语言:javascript
运行
复制
Function CreateFolderRecursive(path As String) As Boolean
    Dim FSO As New FileSystemObject

    'If the path exists as a file, the function fails.
    If FSO.FileExists(path) Then
        CreateFolderRecursive = False
        Exit Function
    End If

    'If the path already exists as a folder, don't do anything and return success.
    If FSO.FolderExists(path) Then
        CreateFolderRecursive = True
        Exit Function
    End If

    'recursively create the parent folder, then if successful create the top folder.
    If CreateFolderRecursive(FSO.GetParentFolderName(path)) Then
        If FSO.CreateFolder(path) Is Nothing Then
            CreateFolderRecursive = False
        Else
            CreateFolderRecursive = True
        End If
    Else
        CreateFolderRecursive = False
    End If
End Function
票数 9
EN

Stack Overflow用户

发布于 2021-02-26 20:32:57

同意MarkD的建议,利用递归,这是我来这里寻找的代码。在提供的路径使用不存在的根文件夹的情况下,这将导致无限循环。添加到MarkD的解决方案中,以检查零长度路径。

代码语言:javascript
运行
复制
Function CreateFolderRecursive(path As String) As Boolean
    Static FSO As FileSystemObject
 
    'Initialize FSO variable if not already setup
    If FSO Is Nothing Then Set lFSO = New FileSystemObject

    'Is the path paramater populated
    If Len(path) = 0 Then
      CreateFolderRecursive = False
      Exit Function
    End If

    'If the path exists as a file, the function fails.
    If FSO.FileExists(path) Then
        CreateFolderRecursive = False
        Exit Function
    End If
 
    'If the path already exists as a folder, don't do anything and return success.
    If FSO.FolderExists(path) Then
        CreateFolderRecursive = True
        Exit Function
    End If
 
    'recursively create the parent folder, then if successful create the top folder.
    If CreateFolderRecursive(FSO.GetParentFolderName(path)) Then
        If FSO.CreateFolder(path) Is Nothing Then
            CreateFolderRecursive = False
        Else
           CreateFolderRecursive = True
        End If
    Else
        CreateFolderRecursive = False
    End If
End Function
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31033820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档