帮助在Windows中使用VBScript创buildFolder1 / Folder2(这两个文件夹不存在之前,我的意思是创build多级文件夹@一个strech。)

我已经创build了使用我的VBScript的文件夹。 当我给一个文件夹的path,该脚本是只创build最后一个文件夹,如果最后一个,但一个文件夹不存在,它会失败…我需要一个VBScript代码来创build整个文件夹结构。 像unix中的mkdir -p

你可以使用这个功能:

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 

或者直接从脚本中调用mkdir命令:

 Set objShell = CreateObject("Wscript.Shell") objShell.Run "cmd /c mkdir X:\folder1\folder2\folder3" 

您必须拆分完整路径并创建每个文件夹。 功能示例:

 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 

在演示后期,但是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