如何使用 vbs 运行 多个批处理并使用环境变量设置批处理文件路径?

How to run more than one batch with vbs and set batch file path using environment variables?

我有这个脚本(由 提供)将 foo.txt 重命名为 foo.bat,启动 foo.bat,当 foo.bat 结束时,重命名它回到 foo.txt.

Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "foo.txt", "foo.bat"

SCRIPT = "foo.bat"
Set objShell = CreateObject("WScript.Shell")
strPath = WScript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 

NewPath = objFSO.BuildPath(strFolder, SCRIPT)
Set objshell = createobject("wscript.shell")

objshell.Run "%COMSPEC% /c " & NewPath, 1, True

' Changes start here
'===================================================================

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")

' Hold execution until cmd.exe process is done
Do 
    ' Get cmd.exe processes
    Set colProcessList = objWMIService.ExecQuery _
        ("SELECT Name FROM Win32_Process WHERE Name LIKE 'cmd.exe'")
    WScript.Sleep 250
Loop While colProcessList.Count > 0

Fso.MoveFile "foo.bat", "foo.txt"

问题是:

foo.txt (foo.bat) 在一个路径中,可以根据 Windows 版本而改变。为此,我需要使用环境变量来设置 foo.txt 路径(例如:%homedrive%),但此更改不起作用。

SCRIPT = "%homedrive%\test\foo.bat"

我需要在第一批结束时调用第二批 (bar.bat) (foo.bat)。但是这种改变在.vbs 的末尾不起作用。

Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "%homedrive%\test\bar.txt", "%homedrive%\test\bar.bat"

SCRIPT = "%homedrive%\test\bar.bat"
Set objShell = CreateObject("WScript.Shell")
strPath = WScript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 

NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")

objshell.Run "%COMSPEC% /c " & NewPath, 1, True

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")

' Hold execution until cmd.exe process is done
Do 
    ' Get cmd.exe processes
    Set colProcessList = objWMIService.ExecQuery _
        ("SELECT Name FROM Win32_Process WHERE Name LIKE 'cmd.exe'")
    WScript.Sleep 250
Loop While colProcessList.Count > 0

Fso.MoveFile "%homedrive%\test\bar.bat", "%homedrive%\test\bar.txt"

这应该有效:

Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
set objshell = createobject("wscript.shell")

homedrive = objshell.ExpandEnvironmentStrings( "%HOMEDRIVE%" )
Fso.MoveFile homedrive & "\test\bar.txt", homedrive & "\test\bar.bat"

SCRIPT = homedrive & "\test\bar.bat"
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 

NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")

objshell.Run (script),1,True

Fso.MoveFile homedrive & "\test\bar.bat", homedrive & "\test\bar.txt"