Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

python - keep multiple console windows open from batch

How can I make a batch file execute multiple (Python) scripts sequentially, each in their own window, and keep all those windows open upon completion? Right now, my batch is something like:

python script1
start python script2
pause/cmd

But only the parent window stays open.

thanks.

Environment: Windows XP/Vista

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you have only two scripts, you had the right idea, just got your syntax wrong:

start cmd.exe /k "python script1.py & start cmd.exe /k python script2.py"

If you need window titles:

start "Window1" cmd.exe /K "python script1.py & start "window2" cmd.exe /K python script2.py"

Any more than two scripts, and you will have to resort to trickier stuff. The following .cmd file will do the trick:

@echo off
if "%~1" == "recurse" goto runScript%~2

start "Window1" cmd /k "%~f0 recurse 1"
exit /b 0

:runScript1
python script1.py
start "Window2" cmd /k "%~f0 recurse 2"
exit /b 0

:runScript2
python script2.py
start "Window3" cmd /k "%~f0 recurse 3"
exit /b 0

:runScript3
python script3.py
exit /b 0

And this is scalable to any number of scripts or commands, with arbitrary parameters to the scripts, etc. If you want the cmd windows to just pause, and disappear when you press a key:

@echo off
if "%~1" == "recurse" goto runScript%~2

start "Window1" cmd /c "%~f0 recurse 1"
exit /b 0

:runScript1
python script1.py
start "Window2" cmd /c "%~f0 recurse 2"
pause
exit /b 0

:runScript2
python script2.py
start "Window3" cmd /c "%~f0 recurse 3"
pause
exit /b 0

:runScript3
python script3.py
pause
exit /b 0

If you want them all to terminate instantly at the press of one key on the final window:

@echo off
if "%~1" == "recurse" goto runScript%~2

start "Window1" cmd /c "%~f0 recurse 1"
exit /b 0

:runScript1
python script1.py
start "Window2" /wait cmd /c "%~f0 recurse 2"
exit /b 0

:runScript2
python script2.py
start "Window3" /wait cmd /c "%~f0 recurse 3"
exit /b 0

:runScript3
python script3.py
pause
exit /b 0

So, you have lots of options for behaviour of the script.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...