使用 VBScript 重命名文件、启动文件、等待并再次重命名 [英] rename a file using VBScript, launch file, wait, and rename again
问题描述
我需要创建一个 vbs 脚本(出于维护目的),将 foo.txt 重命名为 foo.bat 并启动 foo.bat,当 foo.bat 结束时,再次将 foo.bat 重命名为 foo.txt
I need to create a vbs script (for maintenance purposes) that renames foo.txt to a foo.bat and launch foo.bat and when foo.bat ends, rename foo.bat again to foo.txt
这是我的脚本 vbs:
This is my script vbs:
On Error Resume next
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 NewPath, vbHide, true
Fso.MoveFile "foo.bat", "foo.txt"
On Error GoTo 0
脚本运行良好.将 foo.txt 重命名为 foo.bat.启动 foo.bat,但不期望 foo.bat 结束并将其重命名为 foo.txt.
the script executes well. Rename foo.txt to foo.bat. Launches foo.bat, but does not expect foo.bat to end and renames it to foo.txt.
我改变了这一行,没有任何反应:
I changed this line, nothing happens:
objshell.Run NewPath, vbHide, 1, true
我需要什么或我做错了什么?
What do I need or what did I do wrong?
替代解决方案(无 VBScript):(根据 @KenWhite 的建议)
Alternative Solution (no VBScript): (By suggestion of @KenWhite)
代码:
On Error Resume next
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 NewPath, true
On Error GoTo 0
在 foo.bat 的末尾:
And at the end of foo.bat:
ren foo.bat foo.txt
exit
谢谢
推荐答案
这里有一个可能的解决方案,以防万一有人想知道如何在不求助于上述替代方案的情况下解决这个问题.
Here is a possible solution just in case anyone is wondering how to solve this problem without resorting to the alternate proposal mentioned above.
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"
这篇关于使用 VBScript 重命名文件、启动文件、等待并再次重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!