今天说说如何利用fso.createfolder来批量创建文件夹。
代码示例:
Sub FSO创建多级文件夹()
Set fso = CreateObject('Scripting.FileSystemobject')
arr = [a1].CurrentRegion '获取a1的连续区域 存入数组arr
For j = 1 To UBound(arr) '循环每一行
Path = ThisWorkbook.Path & '\' '在此工作薄路径下
If Len(arr(j, 1)) > 0 Then
'工作薄路径下加上j行第一列的单元格值 形成新路径
Path = Path & arr(j, 1)
If Not fso.FolderExists(Path) Then
fso.CreateFolder (Path) '若新路径不存在 则按其创建文件夹
End If
Path = Path & '\'
For i = 2 To UBound(arr, 2) '循环j行的i列
If Len(arr(j, i)) > 0 Then
'在上一级路径的基础上继续增加单元格值 形成新路径
Path = Path & arr(j, i)
If Not fso.FolderExists(Path) Then
'若新路径文件夹不存在,按新路径创建文件夹 此为i级文件夹
fso.CreateFolder (Path)
End If
Path = Path & '\'
Else
Exit For
End If
Next i
End If
Next j
End Sub
联系客服