Showing posts with label File System Object. Show all posts
Showing posts with label File System Object. Show all posts

Wednesday, April 4, 2007

VBScript Fun - Create a File System Directory

Okay, I was stretching for something to write today. So I thought I'd include an entry on my good friend, FileSystemObject, which has been my "go-to" on many occasions.

So, an example...

Say you want to automate the creation of a file system hierarchy, with folders that don't currently exist, and branch pretty deeply. Using the following function, you can pass a long path and the File System Object will create each layer:

makeDirectory "c:\diablopup\testing\a"
  • '''''''''''''''''''''''''''''''''''''''''''''''''''

  • ' This method is responsible for creating a directory

  • ' The directory can be several layers deep, and if the

  • ' parent directories don't exist, it creates them.

  • ' If the directory already exists, it is left alone

  • ''''''''''''''''''''''''''''''''''''''''''''''''

  • sub makeDirectory (newPath)
      Dim myArray,length,count,path,outputFileObject
        Set outputfileObject = CreateObject("Scripting.FileSystemObject")
          if outputfileObject.FolderExists(newPath) then
        • wscript.echo "Dir: "&newPath&" already exists <-- makeDirectory() "

        • exit sub

          end if
            path = ""
            count = 0
            myArray = split(newPath, "\")
            length = ubound(myArray)

            while count <= length
          • path = path + myArray(count) + "\"
          • count = count + 1
          • if not outputfileObject.FolderExists(path) then
            • outputfileObject.CreateFolder(path)
          • end if
            wend
              wscript.echo "Created Dir: "&newPath&" <-- makeDirectory() "
              end sub