VBS – CreateFolder with sub-dir’s (recursion)

Home Forums Scripting Windows Script Host VBS – CreateFolder with sub-dir’s (recursion)

This topic contains 1 reply, has 1 voice, and was last updated by CaTBuS CaTBuS 8 years, 3 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • CaTBuS

    I am having trouble writing a VBScript that will create a Folder with sub-directories (i.e. C:Folder1Folder2 where Folder1 does not yet exist). I know that I have to write a recursive method or loop. I have found many examples online but can’t seem to get any of them to work for me. Below is an example of what I would like to expand.

    If Not objFSO.FolderExists(“C:Folder1Folder2”) then
    End if

    As always, thanks in advance.


    Re: VBS – CreateFolder with sub-dir’s (recursion)

    I will work on an issue for days before posting, and without fail, I will find something minutes after I post. Anyway, I found this little gem and it does work. Having said that, it seems way to verbose. There has to be a cleaner shorter way to do this.

    Set oShell = CreateObject(“WScript.Shell”)
    oSYSDRIV = oShell.Environment(“Volatile”).Item (“SYSTEMDRIVE”)
    WOPM = oSYSDRIV & “PrometheusWOPM”

    ‘// a normal function
    Call CreateDirectoryPath(WOPM)

    ‘// a recursion function
    Call Recursion(WOPM)

    Function CreateDirectoryPath(DirPath)

    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    If objFSO.FolderExists(DirPath) Then
    Exit Function
    End If

    aDirectories = Split(DirPath, “”)
    sCreateDirectory = aDirectories(0)
    For iDirectory = 1 To UBound(aDirectories)
    sCreateDirectory = sCreateDirectory & “” & aDirectories(iDirectory)
    If Not objFSO.FolderExists(sCreateDirectory) Then
    End If
    End Function

    Function Recursion(DirectoryPath)
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)

    If objFSO.FolderExists(DirectoryPath) Then Exit Function
    Call Recursion(objFSO.GetParentFolderName(DirectoryPath))
    End Function

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.