检查文件夹是否存在,如果没有在当前用户登录VBS上创建文件夹

时间:2013-06-04 03:31:38

标签: vbscript createobject create-directory

目前这是我的脚本

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

2 个答案:

答案 0 :(得分:19)

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

在这里你走人,这应该是完美的,尊重丹尼尔。

答案 1 :(得分:1)

这是我的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