How to automatically close cmd window after batch file execution?

后端 未结 8 1523
半阙折子戏
半阙折子戏 2020-11-30 23:47

I\'m running a batch file that has these two lines:

start C:\\Users\\Yiwei\\Downloads\\putty.exe -load \"MathCS-labMachine1\"
\"C:\\Program Files (x86)\\Xmi         


        
相关标签:
8条回答
  • 2020-12-01 00:26

    You could try the somewhat dangerous: taskkill /IM cmd.exe ..dangerous bcz that will kill the cmd that is open and any cmd's opened before it.

    Or add a verification to confirm that you had the right cmd.exe and then kill it via PID, such as this:

    set loc=%time%%random%
    title=%loc%
    for /f "tokens=2 delims= " %%A in ('tasklist /v ^| findstr /i "%loc%"') do (taskkill /PID %%A) 
    

    Substitute (pslist &&A) or (tasklist /FI "PID eq %%A") for the (taskkill /PID %%A) if you want to check it first (and maybe have pstools installed).

    0 讨论(0)
  • 2020-12-01 00:34

    You normally end a batch file with a line that just says exit. If you want to make sure the file has run and the DOS window closes after 2 seconds, you can add the lines:

    timeout 2 >nul
    exit
    

    But the exit command will not work if your batch file opens another window, because while ever the second window is open the old DOS window will also be displayed.

    SOLUTION: For example there's a great little free program called BgInfo which will display all the info about your computer. Assuming it's in a directory called C:\BgInfo, to run it from a batch file with the /popup switch and to close the DOS window while it still runs, use:

    start "" "C:\BgInfo\BgInfo.exe" /popup
    exit
    
    0 讨论(0)
提交回复
热议问题