Check if folder is there, if not create it on current user logged in VBS

后端 未结 2 1851
孤街浪徒
孤街浪徒 2021-02-13 12:15

Currently this is my script

Set oWS = WScript.CreateObject(\"WScript.Shell\")
\' Get the %userprofile% in a variable, or else it won\'t be recognized
userProfile         


        
相关标签:
2条回答
  • 2021-02-13 12:33
    Set oWS = WScript.CreateObject("WScript.Shell")
    ' Get the %userprofile% in a variable, or else it won't be recognized
    userProfile = oWS.ExpandEnvironmentStrings( "%userprofile%" )
    
    Dim objNetwork
    Dim userName
    Dim FSO
    Dim Folder
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Set objNetwork = CreateObject("WScript.Network")
    userName = objNetwork.userName
    
    If NOT (FSO.FolderExists(userProfile + "\AppData\Roaming\Local")) Then
        ' Delete this if you don't want the MsgBox to show
        MsgBox("Local folder doesn't exists, creating...")
        splitString = Split(userProfile, "\")
    
        ' Create folder
        MsgBox("D:\" + splitString(2) + "\AppData\Roaming\Local")
        'FSO.CreateFolder(splitString(2) + "\AppData\Roaming\Local")
    End If
    

    Here you go man, this should work perfect, regards Daniel.

    0 讨论(0)
  • 2021-02-13 12:35

    Here is code part from my utilty for FSO:

    dim ffso
    
    Function GetFSO
        if not IsValidObject(ffso) then set ffso = CreateObject("Scripting.FileSystemObject")
      Set GetFSO = ffso
    End Function
    
    sub SureDirectoryExists(ADir)
        if ADir="" then exit sub
        if not GetFSO().FolderExists(ADir) then
            SureDirectoryExists ffso.GetParentFolderName(ADir)
            ffso.CreateFolder ADir
        end if
    end sub
    
    0 讨论(0)
提交回复
热议问题