使用objfso的带bginfo的vbscript

使用objfso的带bginfo的vbscript,vbscript,environment-variables,Vbscript,Environment Variables,请问如何将用户名设置为变量(例如%userprofile%): 我想这是因为它是一个带有“的字符串 提前谢谢 您需要使用Shell对象来获取userprofile。示例脚本将是 dim objShell, strPath, objFSO, objFile Set objShell = WScript.CreateObject("WScript.Shell") strPath = objShell.ExpandEnvironmentStrings("%UserProfile%") strPath

请问如何将用户名设置为变量
(例如%userprofile%)

我想这是因为它是一个带有
的字符串


提前谢谢

您需要使用
Shell
对象来获取
userprofile
。示例脚本将是

dim objShell, strPath, objFSO, objFile
Set objShell = WScript.CreateObject("WScript.Shell")
strPath = objShell.ExpandEnvironmentStrings("%UserProfile%")
strPath = objFSO.BuildPath(strPath, "AppData\Roaming\Samplefolder\sampletext.txt")

set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(strPath,1)

顺便说一句当我尝试将其更改为“%username%/appdate/…”时,它返回一个错误:“错误评估脚本字段”请在问题中使用代码标记。谢谢。嗨,Pankaj,它在wscript中返回了一个错误--set objShell=wscript.CreateObject(“wscript.Shell”)@Alex-您使用的是ExcelVBA还是VBScript(.vbs)文件?尝试
Set objShell=CreateObject(“Wscript.Shell”)
it's vbscript:)…从CreateObject中删除Wscript成功了,谢谢!然而,我得到了一个新的错误--Object required:“在这一行strPath=objFSO.BuildPath(strPath,“AppData\Roaming\Samplefolder\sampletext.txt”),位置2您是否初始化了
objFSO
?这里是:dim objFSO,objFile,strLine,objShell,strPath set objShell=CreateObject(“Wscript.Shell”)strPath=objShell.ExpandEnvironmentStrings(“%username%”)strPath=objFSO.BuildPath(strPath,“Appdata\Roaming\Avaya\one-X Agent\2.5\Profiles\default\Settings.xml”)Set objFSO=CreateObject(“Scripting.FileSystemObject”)Set objFile=objFSO.OpenTextFile(strPath,1)用于l=1到11的objFile.SkipLine下一个objFile.Skip(38)strLine=objFile.Read(7)objFile.Close如果是InStr(strLine,“222abcd”),则strLine=“No Avaya Ext”如果是echo strLine则结束
dim objShell, strPath, objFSO, objFile
Set objShell = WScript.CreateObject("WScript.Shell")
strPath = objShell.ExpandEnvironmentStrings("%UserProfile%")
strPath = objFSO.BuildPath(strPath, "AppData\Roaming\Samplefolder\sampletext.txt")

set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(strPath,1)