14

目前这是我的脚本

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 不是很熟悉,但这是我可以在使用它的环境中操作的唯​​一平台。

4

2 回答 2

21
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

给你,伙计,这应该很完美,丹尼尔说。

于 2013-06-04T05:25:30.760 回答
2

这是我的 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
于 2018-12-24T17:26:25.027 回答