tags:

views:

1313

answers:

5

Hi.

I've written a batch file to automate some tasks. I can run it from a command window and it runs and displays results. If I double click it from explorer though, it runs and terminates immediately so I can't see the results.

Is there a way I can make batch file window stay open until I dismiss it if I started it by double-clicking the icon?

I don't want to have to pass a /nopause parameter or something when I call a batch file from the command line. I'd like a solution where I can use the batch file without having to do anything too special?

Thanks.

NOTE I don't want it to pause when running from the command line!! I may call this batch file from another batch file to carry out a load of operations. In that case I can't be sitting there to keep hitting enter.

Ideally it would be best if I can put some code in the batch file so it can work out where it was started from, and then pause or not as appropriate.

+11  A: 

Use:

cmd /K myBatch.bat

as your shortcut.

Matthew Flaschen
You could achieve that for *all* batch files by modifying the following registry key: HKEY_CLASSES_ROOT\batfile\shell\open\command.Set the value from "%1" %* to cmd /k "%1" %*
0xA3
Thanks. I like this. I've set it up in the registry as suggested.
Scott Langham
A: 

Hi,

use "pause" in the batch file at the end, and it will wait for the user input

HTH

Anand
This would *always* pause, not only when double-clicking the batch file.
0xA3
A: 

Would the pause command work?

Microsoft Documentation on pause

Rihan Meij
A: 

at the end of file print

pause

it will wait for anykey input

Sergey Kovalenko
doesn't work. It just runs the file and then auto close the window.
Albert T. Wong
A: 

you can just add params to the batch call and handle conditionally pause statement in your batch. So when started from command line or dblclick the batch can pause, when called from others batches with a /nopause param don't pause.

Andrea Balducci
"I don't want to have to pass a /nopause parameter or something when I call a batch file from the command line." was stated in the question.
0xA3
have you read my response? the param is "when called from others batches" not when called from cmd line!
Andrea Balducci
parameters in batch are the default way, since msdos. What if you share your script with devs having no admin rights to tweak the registry? Is this an out of the box approach?
Andrea Balducci