編輯:我會盡量讓我更清楚我真正想要的。
我有一些很長的批處理檔案,我想按順序運行,并且僅在前一個完成后才開始運行。
我正在嘗試使用主批處理檔案來控制它
我希望他們在自己的視窗中啟動每個視窗,該視窗在完成后仍保持打開狀態,以便稍后回顧
按數字順序:
1.運行主批處理檔案
2.打開新的cmd視窗
3.運行批處理檔案1
4.等待1完成
5.1 完成,保持視窗打開
6.打開另一個新的cmd視窗
7.運行batch2等
- 原始資訊 -
嗨,我有一個 Windows 批處理檔案,它需要按順序運行其他 Windows 批處理檔案,并在開始下一個之前等待它們完成。
就像是:
@echo off
setlocal EnableDelayedExpansion
SET RUN="C:first.bat"
start /wait cmd /c %RUN%
SET RUN="C:second.bat"
start /wait cmd /c %RUN%
例如,第一個和第二個只是回顯如下內容:
@echo off
echo 1st
exit /b 0
當我運行它時,它會在新視窗中啟動第一個腳本,并在完成后像我想要的那樣保持視窗打開,但要進入第二個腳本,我必須關閉新的 cmd 視窗。
如何讓主批處理腳本在不關閉 first.bat cmd 視窗的情況下啟動 second.bat?
謝謝
uj5u.com熱心網友回復:
這是一個非常不尋常的要求
- 其他批處理檔案不應由
cmd.exe處理主批處理檔案的實體處理,而應由其他實體處理,cmd.exe只是為了在不同的控制臺視窗中輸出它們,并且 - 在完成批處理檔案的處理后保持彼此的命令列程運行,以便能夠在它們的控制臺視窗中查看它們的輸出和
- 等待主批處理檔案的進一步處理,直到啟動的單獨命令列程完成批處理檔案的處理,但不要等待已啟動的其他命令列程的終止。
Windows 命令處理器cmd.exe不是為批處理檔案的序列化多處理而設計的,它具有多個自身的實體,最終應該繼續運行。
然而,這里有三個批處理檔案證明這是可能的,但有一個缺點稍后解釋。
主檔案
@echo off & goto Main
:WaitForBatch
start "Run %~nx1" %ComSpec% /D /S /K "(%ComSpec% /D /C "%~1") & title Finished %~n1"
echo Waiting for finished execution of "%~nx1" ...
:CheckBatchRun
%WaitCommand% >nul
%SystemRoot%\System32\tasklist.exe /NH /FI "IMAGENAME eq cmd.exe" /V | %SystemRoot%\System32\find.exe /I "%~nx1" >nul
if errorlevel 1 goto :EOF
goto CheckBatchRun
:Main
setlocal EnableExtensions DisableDelayedExpansion
title Run %~nx0
if exist %SystemRoot%\System32\timeout.exe (
set "WaitCommand=%SystemRoot%\System32\timeout.exe /T 1"
) else (
set "WaitCommand=%SystemRoot%\System32\ping.exe 127.0.0.1 -n 2"
)
call :WaitForBatch "%~dp0First.bat"
call :WaitForBatch "%~dp0Second.bat"
title Finished %~n0
pause
endlocal
首先.bat
@echo off
dir %SystemRoot% /B /S
第二個.bat
@echo off
dir %SystemRoot%\*.exe
echo/
pause
Main.bat以期望的方式編碼First.bat并與Second.bat位于同一目錄中Main.bat,但其他批處理檔案也可以位于不同的目錄中。三個批處理檔案執行的當前目錄可以是任何目錄。
Main.bat 首先為自己設定執行環境,即:
- 命令回顯模式關閉并
- 命令擴展啟用和
- 延遲擴展禁用。
cmd.exe處理控制臺視窗的視窗標題Main.bat修改為Run Main.bat開頭顯示。
接下來確定命令TIMEOUT是否可用,在這種情況下,此命令稍后將用于延遲一秒(Windows Vista 和更高版本的 Windows 客戶端版本),或者命令PING 是否必須用于延遲一秒(Windows XP)。
Then the subroutine WaitForBatch is called the first time with the batch file First.bat.
The subroutine uses the command START to start one more command process in a new console window with window title Run First.bat with ignoring the AutoRun registry string value which by Windows default does not exist.
This second cmd.exe instance keeps running after execution of the command line specified next with the other arguments is finished. The command line for second cmd.exe requires the execution of a third cmd.exe again with ignoring AutoRun registry string value if existing at all to execute the batch file First.bat specified with fully qualified file name. The third cmd.exe instances closes itself on finishing processing of the batch file.
The second cmd.exe changes now the title of its console window to Finished First. Important is here that the batch file extension is not anymore in the window title.
The first cmd.exe instance processing Main.bat continues with batch file processing already after successful start of second cmd.exe. It uses the command to wait one second and then runs TASKLIST to output all running cmd.exe processes with verbose information which is redirected to command FIND to search case-insensitive for the batch file name First.bat.
As long as there is a cmd.exe process running with First.bat, the first cmd.exe continues batch file processing of Main.bat inside subroutine WaitForBatch in a loop with a jump to CheckBatchRun. Otherwise the subroutine is left and processing of Main.bat continues with the second CALL of WaitForBatch with Second.bat.
Finally Main.bat changes also its window title to Finished Main and prompts the user to press any key in case of Main.bat execution was started with a double click on this file in Windows Explorer.
First.bat takes a very long time to finish as thousands of file names must be output into the console window of second cmd.exe. It is possible to click on the X symbol of console window with title Run First.bat to terminate immediately the execution of second and of third cmd.exe which results in first cmd.exe continues with starting two more cmd.exe for processing Second.bat.
It is also possible to interrupt the long running First.bat by pressing Ctrl C and answer the prompt for termination of batch job with Y (on English Windows) resulting in third cmd.exe stopping really the processing of First.bat and second cmd.exe keeps running showing the output of First.bat and changing the window title of its console window to Finished First. This is detected by first cmd.exe processing Main.bat and it starts the processing of Second.bat.
The disadvantage of this solution is that on pressing Ctrl C in console window with title Run First.bat while DIR outputs all the file names and pressing now N (on English Windows) results nevertheless in a termination of the batch job. I am not 100% sure why this happens. I have just a supposition for this behavior.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?cmd /?dir /?echo /?endlocal /?find /?goto /?if /?pause /?ping /?setlocal /?start /?tasklist /?timeout /?title /?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/340614.html
