Help To create Folder1/Folder2 in Windows using VBScript ( Both the folders not exists before, i mean to create multilevel folders @ a strech.)

前端 未结 4 837
既然无缘
既然无缘 2020-12-11 01:39

I have created folders using my VBscript. when i give a folder path, the script is creating only the last folder, if the last but one folder does not exists, it will fail...

相关标签:
4条回答
  • 2020-12-11 01:50

    Not disagreeing with other answers, but checking if each folder exists is also a good idea - That way it doesn't throw an error when you try to create a folder that already exists

    Sub ensureFolderExists(strFldrPath)
        If Not FSO.FolderExists(strFldrPath) AND strFldrPath <> "" Then
            ensureFolderExists(FSO.GetParentFolderName(strFldrPath))
            FSO.CreateFolder strFldrPath
        End If
    End Sub
    
    0 讨论(0)
  • 2020-12-11 01:56

    Late to the show, but the Shell.Application object works for me in XP, as follows ...

    with CreateObject("Shell.Application")
      set oFolder = .NameSpace("C:\")
      if (not oFolder is nothing) then oFolder.NewFolder("a\b\c\d")
    end with
    
    0 讨论(0)
  • 2020-12-11 01:59

    You could use this function:

    Const PATH = "X:\folder0\folder1\folder2"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    BuildFullPath PATH
    
    Sub BuildFullPath(ByVal FullPath)
        If Not fso.FolderExists(FullPath) Then
            BuildFullPath fso.GetParentFolderName(FullPath)
            fso.CreateFolder FullPath
        End If
    End Sub
    

    Or simply call the mkdir command from your script:

    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run "cmd /c mkdir X:\folder1\folder2\folder3"
    
    0 讨论(0)
  • 2020-12-11 02:07

    You must split the full path and create each folder. Example function:

    Function CreateFolderRecursive(FullPath)
      Dim arr, dir, path
      Dim oFs
    
      Set oFs = WScript.CreateObject("Scripting.FileSystemObject")
      arr = split(FullPath, "\")
      path = ""
      For Each dir In arr
        If path <> "" Then path = path & "\"
        path = path & dir
        If oFs.FolderExists(path) = False Then oFs.CreateFolder(path)
      Next
    End Function
    
    0 讨论(0)
提交回复
热议问题