windows Help To create Folder1/Folder2 using VBScript (这两个文件夹以前都不存在,我的意思是创建多级文件夹@a strech。)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4407386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Help To create Folder1/Folder2 in Windows using VBScript ( Both the folders not exists before, i mean to create multilevel folders @ a strech.)
提问by Vijay Athreyan
I have created folders using my VBscript. when i give a folder path, the script is creating only the last folder, if the last but one folder does not exists, it will fail... I need a vbscript code to create the entire folder structure on the single go. like mkdir -p in unix
我已经使用我的 VBscript 创建了文件夹。当我给出一个文件夹路径时,脚本只创建最后一个文件夹,如果最后一个文件夹不存在,它将失败......我需要一个 vbscript 代码来一次性创建整个文件夹结构。像 unix 中的 mkdir -p
回答by Pascal Rodriguez
You could use this function:
你可以使用这个功能:
Const PATH = "X:\folder0\folder1\folder2"
Set fso = CreateObject("Scripting.FileSystemObject")
BuildFullPath PATH
Sub BuildFullPath(ByVal FullPath)
If Not fso.FolderExists(FullPath) Then
BuildFullPath fso.GetParentFolderName(FullPath)
fso.CreateFolder FullPath
End If
End Sub
Or simply call the mkdir command from your script:
或者简单地从脚本中调用 mkdir 命令:
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "cmd /c mkdir X:\folder1\folder2\folder3"
回答by wickie79
You must split the full path and create each folder. Example function:
您必须拆分完整路径并创建每个文件夹。示例函数:
Function CreateFolderRecursive(FullPath)
Dim arr, dir, path
Dim oFs
Set oFs = WScript.CreateObject("Scripting.FileSystemObject")
arr = split(FullPath, "\")
path = ""
For Each dir In arr
If path <> "" Then path = path & "\"
path = path & dir
If oFs.FolderExists(path) = False Then oFs.CreateFolder(path)
Next
End Function
回答by Red
Late to the show, but the Shell.Application object works for me in XP, as follows ...
迟到了,但 Shell.Application 对象在 XP 中对我有用,如下...
with CreateObject("Shell.Application")
set oFolder = .NameSpace("C:\")
if (not oFolder is nothing) then oFolder.NewFolder("a\b\c\d")
end with
回答by Mitchell Bland
Not disagreeing with other answers, but checking if each folder exists is also a good idea - That way it doesn't throw an error when you try to create a folder that already exists
不反对其他答案,但检查每个文件夹是否存在也是一个好主意 - 这样当您尝试创建一个已经存在的文件夹时它不会抛出错误
Sub ensureFolderExists(strFldrPath)
If Not FSO.FolderExists(strFldrPath) AND strFldrPath <> "" Then
ensureFolderExists(FSO.GetParentFolderName(strFldrPath))
FSO.CreateFolder strFldrPath
End If
End Sub