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

问题描述 投票:0回答:1

这个问题在这里已有答案:

我有这个脚本(由@WesternSage提供)将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.txtfoo.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"
batch-file vbscript
1个回答
1
投票

这应该有效:

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"
© www.soinside.com 2019 - 2024. All rights reserved.