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, 1 month ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • CaTBuS
    CaTBuS
    Participant
    #155258

    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.

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

    As always, thanks in advance.

    CaTBuS
    CaTBuS
    Participant
    #309108

    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.

    Code:
    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
    objFSO.CreateFolder(sCreateDirectory)
    End If
    Next
    End Function

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

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

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

You must be logged in to reply to this topic.

Register for this Petri Webinar!

Want to Make Your Backup Storage Unlimited & Ready for the Cloud? – Free Thurrott Premium Account with Webinar Registration!

Tuesday, August 27, 2019 @ 1:00 pm EDT

A Scale-Out Backup storage infrastructure is a must-have technology for your backups. In this webinar, join expert Rick Vanover for a look on what real-world problems are solved by the Scale-Out Backup Repository.

Register Now

Sponsored By