2013-06-04 37 views
10

目前,這是我的腳本檢查文件夾是存在的,如果不登錄的VBS當前用戶創建

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

我所試圖做的就是抓住當前用戶登錄,我想它來檢查目錄D:\「personsuser」\ Appdata \ Roaming \ Local來查看是否創建了文件夾「Local」,如果未創建,我想通過vbs中的createobject創建一個。從我所知道的上面的腳本抓住當前登錄的用戶,但是我不知道如何使用這個變量來創建一個文件夾。

我知道我將不得不沿着這些路線包括東西:

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFolder = objFSO.CreateFolder("C:\FSO") 

而且什麼沿着這些線路:

Dim objNetwork 
Dim userName 
Dim FSO 

Set FSO = CreateObject("Scripting.FileSystemObject") 

Set objNetwork = CreateObject("WScript.Network") 
userName = objNetwork.userName 

If fso.driveExists("D:\" & userName & "\AppData\Local\") Then 
    FSO.CreateDirectory ("D:\" & userName & "\AppData\Local\") 
End If 

在此先感謝,不是很熟悉VBS但是那是隻有我可以在我使用它的環境中運行平臺。

+0

應該自動創建用戶配置文件中的「本地」子文件夾。如果不是,你應該調查什麼阻止了它的創建並解決這個問題。 –

回答

17
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 

在這裏,你去的人,這應該工作完美,視爲丹尼爾。

相關問題