【问题标题】:Creating sub folders using a path使用路径创建子文件夹
【发布时间】:2023-02-15 17:50:45
【问题描述】:

下面是我从单元格 E3 上的路径创建子文件夹 A 的代码。这是路径:C:\SW\A。但是,如果我想使用 C:\SW\A\B\C 之类的 1 个路径创建这些子文件夹(A、B 和 C)怎么办?这不会创建子文件夹。

Sub MakeFolders()

 Dim path As String
'mkdir function
 path = Range("E3").Value 
MkDir path

End Sub

知道如何仅使用 1 个路径制作 3 个子文件夹吗?

【问题讨论】:

标签: excel vba directory path mkdir


【解决方案1】:

Win32API,MakeSure目录存在, 将执行您的要求 - 它将检查路径中的每个文件夹是否存在,如果不存在,它将创建它。要使用 API,您需要确保 Declaration 组件位于模块的开头(连同其附带的功能,构建目录路径)

#If VBA7 Then
    Private Declare PtrSafe Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal DirPath As String) As Long
#Else
    Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal DirPath As String) As Long
#End If

Function BuildDirPath(ByVal Path As String) As Boolean
    BuildDirPath = CBool(MakeSureDirectoryPathExists(Path) = 1)
End Function

要使用它,只需调用函数 BuildDirPath 即可。

Debug.Print BuildDirPath("C:SWABC")

如果成功,它将返回 TRUE 值。我应该补充一点,这个 API 仅限于 ASCII 字符并且不支持 Unicode - 这意味着它不能用于非西方字符集(例如日语平假名).希望有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 2012-04-22
    • 2012-03-16
    • 2017-08-06
    相关资源
    最近更新 更多