当前这是我的脚本
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 & "AppDataLocal") Then
FSO.CreateDirectory ("D:" & userName & "AppDataLocal")
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 + "AppDataRoamingLocal")) 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) + "AppDataRoamingLocal")
'FSO.CreateFolder(splitString(2) + "AppDataRoamingLocal")
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
此函数将在路径参数(字符串(中创建所有文件夹。
Public Function CheckCreateFolder(path)
Dim TempPath As String
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
pos = 0
While pos < Len(path)
pos = InStr(pos + 1, path, "")
TempPath = Left(path, pos)
If Not (FSO.FolderExists(TempPath)) Then
FSO.CreateFolder (TempPath)
End If
Wend
End Function