问题描述
我有这个脚本(由提供) @WesternSage )将foo.txt
重命名为foo.bat
,启动foo.bat
,并在foo.bat
结束时将其重命名为foo.txt
.
I have this script (courtesy of @WesternSage) that renames foo.txt
to foo.bat
, launches foo.bat
, and when foo.bat
ends, renames it back to 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%
),但是此更改不起作用.
foo.txt
(foo.bat
) is in a path, which can change depending on the Windows version. For this I need to use environment variables to set the foo.txt
path (example: %homedrive%
), but this change doesn't work.
SCRIPT = "%homedrive%\test\foo.bat"
当第一个批次结束(foo.bat
)时,我需要呼叫第二个批次(bar.bat
).但是,此更改在.vbs末尾无效.
I need to call a second batch (bar.bat
), when the first one ends (foo.bat
). But this change does not work at the end of .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"
这篇关于如何使用vbs运行多个批处理并使用环境变量设置批处理文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!