问题
I have this batch file:
SET WINRAR="C:\Program Files (x86)\WinRAR"
start "" %WINRAR%\WinRAR.exe a -u -m5 "Group 1.rar" "Group 1" "Group 1"
start "" %WINRAR%\WinRAR.exe a -u -m5 "Group 2.rar" "Group 2" "Group 2"
start "" %WINRAR%\WinRAR.exe a -u -m5 "Group 3.rar" "Group 3" "Group 3"
start "" "D:\"
I want all the rar processes to work at the same time and to open the directory D:\
after rar finishes.
回答1:
You can achieve this by starting your un-rar processes without /WAIT and check whether they're finished using tasklist
:
@ECHO OFF
SET WINRAR="C:\Program Files (x86)\WinRAR"
start "" %WINRAR%\WinRAR.exe a -u -m5 "Group 1.rar" "Group 1" "Group 1"
start "" %WINRAR%\WinRAR.exe a -u -m5 "Group 2.rar" "Group 2" "Group 2"
start "" %WINRAR%\WinRAR.exe a -u -m5 "Group 3.rar" "Group 3" "Group 3"
:LOOP
tasklist /FI "IMAGENAME eq WinRAR.exe" 2>NUL | find /I /N "WinRAR.exe">NUL
if %ERRORLEVEL%==0 (
ping localhost -n 6 >nul
GOTO LOOP
)
start "" "D:\"
The code does the following:
First you start all your un-rar processes. Then you check whether your tasklist contains any winrar.exe processes. If it is the case you wait 5 seconds and check again. As soon as there are no more winrar.exe entries in the task list you go on to start "" "D:\"
.
EDIT: As you've asked what how wait by ping works, here is the explanation.
Sorry, there was a bug in my code. It's ping localhost -n 6
and not ping localhost -6
.
ping localhost -n 6>nul
makes your system ping localhost 6 times with 1 second between each ping. 6 pings with 1 second between is 5 :) As localhost responds within 1 ms you are waiting about 5 seconds. >nul
suprsses the output of the ping command in your console.
回答2:
I'm afraid the solution is not affordable from a batch file only. The options you have pass by thinking outside the box and implement a program who do the job.
For instance, a good implementation could be using the app.activate method who allow you to check if there is a window with certain name "alive".
If you opt for following this path, the solution would be to create a vbs file called "waitForAll.vbs" (for instance) and provide something like this:
Dim objShell
dim found
dim nCount
Set objShell = CreateObject("WScript.Shell")
found = true
nCount = 100 ' to avoid hangings
do while found and nCount > 0
found = objShell.appActivate("CAPTION OF YOUR WINRAR WINDOWS")
wscript.sleep 5 * 1000 ' sleep for five seconds
nCount = nCount - 1
loop ' found
if not found then
' launch whatever
end if ' not found
And instead of that start "" d:\ you have to insert a call to this "waitForAll.vbs" program: the program will look for all the windows in the userspace that are called similar that the ones of the winrar: when they aren't found, it will start whatever you want.
Optionally, if a winrar window hangs (with a popup message or something like that), this program eventually end via the nCount counter.
来源:https://stackoverflow.com/questions/30368450/how-to-make-batch-wait-for-multiple-subprocesses