Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically close cmd window after batch file execution?

Tags:

batch-file

cmd

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)\Xming\Xming.exe" :0 -clipboard -multiwindow 

This batch file is used to run the Xming application and then the PuTTY app so I can SSH into my university's computer lab.

However, if I run this and Xming is not already open, once I exit from the PuTTY terminal the cmd window remains open. Only if I have already run Xming does the cmd window close when I close the PuTTY terminal. I've tried adding exit to the last line of the batch file, but to no avail.

like image 799
yiwei Avatar asked Feb 04 '13 23:02

yiwei


People also ask

How do I close a command window after a batch file?

If the batch file was launched from a shortcut, "exit /b" should work fine.

How do I close a command prompt window?

To close or exit the Windows command line window, also referred to as command or cmd mode or DOS mode, type exit and press Enter . The exit command can also be placed in a batch file. Alternatively, if the window is not fullscreen, you can click the X close button in the top-right corner of the window.

How do you close cmd window after execute in C#?

Arguments = "/k " + Command + " & exit"; But if you read the "cmd /?", you'll see that the purpose of "/k" argument is to keep the window. So if it's not what you want, just use the "/c" argument instead.

What is K in cmd?

Alternatively referred to as Cmd+K, Command+K is a keyboard shortcut that varies depending on the program used. For example, in certain programs, Command+K is used to insert a hyperlink, and in some browsers, Command+K focuses on the search bar. Tip. On Windows computers, the most similar keyboard shortcut is Ctrl + K ...


2 Answers

Modify the batch file to START both programs, instead of STARTing one and CALLing another

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

If you run it like this, no CMD window will stay open after starting the program.

like image 103
Ahmad Avatar answered Oct 06 '22 11:10

Ahmad


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 
like image 20
Adrian747 Avatar answered Oct 06 '22 11:10

Adrian747