检查文件夹是否存在,如果没有在当前登录 VBS 的用户上创建它 [英] Check if folder is there, if not create it on current user logged in 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",如果没有't created 我想通过 vbs 中的 createobject 创建一个.我所知道的上面的脚本抓取了当前登录的用户,但是我不确定如何使用这个变量来创建一个文件夹.
What I am trying to do is grab the current user logged in, I want it to check the directory D:\"personsuser"\Appdata\Roaming\Local to see if the folder "Local" is created, if it isn't created I want to create one via createobject in vbs. The script above from what i know grabs the current logged on user, however i'm not sure how to use this variable to create a folder.
我知道我必须按照以下方式整合一些东西:
I know i will have to incorporate something along these lines:
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 不是很熟悉,但这是我在使用它的环境中唯一可以操作的平台.
Thanks in advance, not very familiar with VBS however that is the only platform I can operate from in the environment that i'm using it.
推荐答案
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
开始吧伙计,这应该很完美,Daniel.
Here you go man, this should work perfect, regards Daniel.
这篇关于检查文件夹是否存在,如果没有在当前登录 VBS 的用户上创建它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!