Vbscript 检查文件夹是否存在,如果不存在,请在当前登录VBS的用户上创建该文件夹
目前这是我的脚本Vbscript 检查文件夹是否存在,如果不存在,请在当前登录VBS的用户上创建该文件夹,vbscript,createobject,create-directory,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:\“PersonUser”\Appdata\Roaming\Local
Set oWS = WScript.CreateObject("WScript.Shell")
' Get the %userprofile% in a variable, or else it won't be recognized
userProfile = oWS.ExpandEnvironmentStrings( "%userprofile%" )
我想做的是抓取当前登录的用户,我想让它检查目录D:\“PersonUser”\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
或者沿着这些路线:
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不是很熟悉,但这是我在使用它的环境中唯一可以使用的平台
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
给你,伙计,这应该很好用,丹尼尔。这是我为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
应自动创建用户配置文件中的
Local
子文件夹。如果不是,您应该调查是什么阻止了它的创建并修复它。