JSI Tip 10441. How can VBScript create multiple folders in a path, like the MkDir command?

Jerold Schulman

April 26, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today


The MkDir (MD) command will create multiple folders in a path. If you want to create C:Folder1Folder2Folder3Folder4, MakeDir will create them all.

In VBScript, the CreateFolder method will only create one folder.

I have scripted MakeDir.vbs to emulate the MkDir command.

The syntax for using MakeDir.vbs is:

cscript //nologo MakeDir.vbs FolderPath

Where FolderPath is the Fully Qualified Folder Path, like "C:Documents and SettingsJerryMy DocumentsMy Home Owners AssociationPublic Records".

MakeDir.vbs contains:

dim objArguments, ObjSet objArguments = Wscript.ArgumentsIf WScript.Arguments.Count = 0 then   Wscript.Echo "Syntax: cscript //nologo MakeDir.vbs FolderPath"   Wscript.QuitEnd IfObj = objArguments(0)X = MakeDir(Obj)Wscript.QuitFunction MakeDir (strPath)Dim strParentPath, objFSO  Set objFSO = CreateObject("Scripting.FileSystemObject")On Error Resume NextstrParentPath = objFSO.GetParentFolderName(strPath)  If Not objFSO.FolderExists(strParentPath) Then MakeDir strParentPathIf Not objFSO.FolderExists(strPath) Then objFSO.CreateFolder strPathOn Error Goto 0   MakeDir = objFSO.FolderExists(strPath)End Function



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like