keep multiple console windows open from batch

后端 未结 3 740
醉话见心
醉话见心 2020-12-06 14:31

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 i

3条回答
  •  旧巷少年郎
    2020-12-06 15:35

    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.

提交回复
热议问题