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

时间:2019-02-25 20:51:52

标签: batch-file vbscript

我有这个脚本(由@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"

1 个答案:

答案 0 :(得分: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"