tags:

views:

43

answers:

2

I like to have a final PAUSE in my *.bat scripts so I can just double click on them in Windows explorer and have the chance to read the output. However, the final PAUSE is an annoyance when I run the same script from the command line.

Is there any way to detect whether we are running the script from a command prompt (or not) and insert the PAUSE (or not) accordingly?

(Target environment is Windows XP and greater.)

Update

I've managed to compose this from Anders's answer:

(((echo.%cmdcmdline%)|find /I "%~0")>nul)
if %errorlevel% equ 0 (
    set GUI=1
) else (
    set CLI=1
)

Then, I can do stuff like this:

if defined GUI pause
+3  A: 

I doubt that there's a distinction, because I think it just starts a command prompt and then runs the bat when you double click on it.

However, if you make shortcuts to the bat files and go to Properties and add in an extra argument (something like "/p") in the "Target" field, then you could check for the presence of that argument at the end of the script and pause if it is set. Then, running from the shortcut would cause it to end in a pause and running from command line wouldn't.

Tom Smilack
I admit it's not a very ugly hack...
Álvaro G. Vicario
Anders
+4  A: 
@echo off
echo.Hello World
(((echo.%cmdcmdline%)|find /I "%~0")>nul)&&pause

...NT+ only, no %cmdcmdline% in Win9x probably

Anders
Superb! Thanks a lot.
Álvaro G. Vicario
While it's not perfect (there's another scenario, scripts ran from other scripts) it's basically what I had in mind.
Álvaro G. Vicario