Side note: Don't use the Dir function, it is buggy. Instead, use the FileSystem library wich is more reliable and have stronger meaning.
Sample code:
Dim fso As Object '// Scripting.FileSystemObjectSet fso = CreateObject("Scripting.FileSystemObject") '// check if a directory existIf Not (fso.FolderExists("c:\myfolder")) Then '// create a directory fso.CreateFolder ("c:\myFolder")End If
And I tend to use a small recursive routine to create folders, something like this example. It takes care of creating any missing parent folders in the path along with the lowest node new folder. Since it is recursive I like to create the filesystem object outside of it and then just pass in the object for its usage.
Sub Main() ' Create filesystem object Dim FSO As Object Set FSO = CreateObject("Scripting.FileSystemObject") ' Create new folder(s) MakeDir "c:\temp\dir1\dir2\dir3", FSOEnd SubSub MakeDir(ByVal DirectoryPath As String, ByVal FSO As Object) ' Recursive routine to create a folder and any needed parent folde(s) If Not FSO.FolderExists(DirectoryPath) Then MakeDir FSO.GetParentFolderName(DirectoryPath), FSO FSO.CreateFolder DirectoryPath End IfEnd Sub
Don't use the Dir function, it is buggy.
Instead, use the FileSystem library wich is more reliable and have stronger meaning.
Sample code:
Open in new window