Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to avoid cmd will be closed after a batch was executed?

Tags:

batch-file

cmd

I have a batch file with for example

cd c:\test

But there is more then this in the batch, it's just an example.

So, if I call it from explorer, I see a cmd window for short time and then it will be closed. How can I avoid this?

I tried to call PAUSE at the end, but for me it's cheating :)

UPD:

just imagine, I'm lazy and need to call different batches from different directories very often. That's why I wanted to have my batches in just one diretory calling another batches, but I also want to see results after execution

like image 363
Tima Avatar asked Jan 10 '12 16:01

Tima


People also ask

How do I keep CMD open after execution?

Type the "cmd /k" parameter before every command to keep the window from closing.

How do I run a batch file continuously?

Pressing "y" would use the goto command and go back to start and rerun the batch file. Pressing any other key would exit the batch file.

How do I close a batch file without closing the window?

Batch file processing ends when execution reaches the end of the batch file. The trick therefore is to use the goto command to jump to a label right before the end of the file, so that execution “falls off the end”.


1 Answers

I presume you are double clicking on the batch file from within Windows Explorer (or else right clicking and selecting open). If so, then you are getting the expected behavior. When you double click on any executable, the window should close once the executable completes.

Putting a PAUSE before the end is the appropriate technique to allow you to see the results before the Window closes. It is not cheating.

If you want the command window to remain open after the batch file terminates, then you can use an appropriately configured shortcut.

Assume the batch file is C:\MyPath\TEST.BAT. You create a shortcut for it, right mouse click on the shortcut, and edit properties. Modify the Target: as follows:

cmd /k "C\MyPath\TEST.BAT"

If you double click on the shortcut, TEST.BAT will run and the command window will remain open after it terminates. But now the command window will remain open indefinitely until you explicitly close it.

like image 126
dbenham Avatar answered Sep 18 '22 07:09

dbenham