Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for md.vbs

(view source code of md.vbs as plain text)

  1. Option Explicit
  2.  
  3. ' UNC path
  4. CreateDirs "\\MYSERVER\D$\Test01\Test02\Test03\Test04"
  5. ' Absolute path
  6. CreateDirs "D:\Test11\Test12\Test13\Test14"
  7. ' Relative path
  8. CreateDirs "Test21\Test22\Test23\Test24"
  9.  
  10.  
  11. Sub CreateDirs( MyDirName )
  12. ' This subroutine creates multiple folders like CMD.EXE's internal MD command.
  13. ' By default VBScript can only create one level of folders at a time (blows
  14. ' up otherwise!).
  15. '
  16. ' Argument:
  17. ' MyDirName   [string]   folder(s) to be created, single or
  18. '                        multi level, absolute or relative,
  19. '                        "d:\folder\subfolder" format or UNC
  20. '
  21. ' Written by Todd Reeves
  22. ' Modified by Rob van der Woude
  23. ' http://www.robvanderwoude.com
  24.  
  25. 	Dim arrDirs, i, idxFirst, objFSO, strDir, strDirBuild
  26.  
  27. 	' Create a file system object
  28. 	Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  29.  
  30. 	' Convert relative to absolute path
  31. 	strDir = objFSO.GetAbsolutePathName( MyDirName )
  32.  
  33. 	' Split a multi level path in its "components"
  34. 	arrDirs = Split( strDir, "\" )
  35.  
  36. 	' Check if the absolute path is UNC or not
  37. 	If Left( strDir, 2 ) = "\\" Then
  38. 		strDirBuild = "\\" & arrDirs(2) & "\" & arrDirs(3) & "\"
  39. 		idxFirst    = 4
  40. 	Else
  41. 		strDirBuild = arrDirs(0) & "\"
  42. 		idxFirst    = 1
  43. 	End If
  44.  
  45. 	' Check each (sub)folder and create it if it doesn't exist
  46. 	For i = idxFirst to Ubound( arrDirs )
  47. 		strDirBuild = objFSO.BuildPath( strDirBuild, arrDirs(i) )
  48. 		If Not objFSO.FolderExists( strDirBuild ) Then 
  49. 			objFSO.CreateFolder strDirBuild
  50. 		End if
  51. 	Next
  52.  
  53. 	' Release the file system object
  54. 	Set objFSO= Nothing
  55. End Sub
  56.  

page last modified: 2024-04-16; loaded in 0.0080 seconds