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
Asked
Active
Viewed 4.7k times
4 Answers
43
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:
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "cmd /c mkdir X:\folder1\folder2\folder3"
Cid
- 14,374
- 4
- 25
- 43
Pascal Rodriguez
- 961
- 9
- 12
-
Thanks a lot for the Reply Pascal – Vijay Athreyan Dec 10 '10 at 10:10
-
To make it work with relative paths, the conditional can be changed to `If path <> "" and not objFSO.FolderExists(path)` – Oriol May 27 '14 at 14:27
-
1@VijayAthreyan, shouldn't you accept the answer, since it is correct? – lpacheco Aug 11 '15 at 17:31
-
@Pascal Rodriguez, the second option works, but if you make the directory structure a parameter you create a vulnerability where the caller can inject a shell command, isn't it? – lpacheco Aug 11 '15 at 17:33
-
Option 2 has to contend with spaces in the folder name so it needs to be enclosed in quotes ... something like this should be done: Shell.Run "cmd /c mkdir """ & PATH & """" – Mike Q Jan 11 '21 at 17:09
10
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
wickie79
- 470
- 1
- 3
- 7
2
Late to the show, but the Shell.Application object works for me in XP, as follows ...
with CreateObject("Shell.Application")
set oFolder = .NameSpace("C:\")
if (not oFolder is nothing) then oFolder.NewFolder("a\b\c\d")
end with
Red
- 21
- 1
1
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
Mitchell Bland
- 56
- 4
-
Only posting for anyone who stumbles upon this thread in the future – Mitchell Bland Jul 18 '18 at 13:29
-
The `If Not FSO.FolderExists(FSO.GetParentFolderName(strFldrPath)) then` check is completely redundant. Just call `ensureFolderExists` on parent you already have existence check baked in. You might want to check if `strFldrPath` is empty string instead. – wqw Nov 23 '18 at 11:13
-