11

The last line in my batch file is pause. Is there any way to add a if condition to see if the script is run within command prompt or by double clicking to execute? I want to skip pause if it's running in command prompt.

...
...
if not RUN_IN_COMMAND_PROMPT (
  pause
)

EDIT: Hope to find a solution works in Windows Server 2003/2008, WinXP, Win7.

0

4 Answers 4

8
CALL :GETMYSWITCH %CMDCMDLINE%
IF /I "%MYSWITCH%" == "/C" ECHO I WAS STARTED IN THE EXPLORER & PAUSE
IF /I NOT "%MYSWITCH%" == "/C" ECHO I WAS STARTED IN A DOS SESSION


:GETMYSWITCH
SET MYSWITCH=%2
4

I know this is a year later but for future people searching you can use

If /I "%COMSPEC%" == %CMDCMDLINE% Goto SkipPause
pause
:SkipPause

It will skip the pause block if running from the command line and pause if running from batch file.

1

By definition, a shell script is always going to be run in a "command prompt". But try using the SESSIONNAME env var - it seems to NOT be present if the script was started by double-clicking instead of manually running it from a prompt.

3
  • 1
    Tested under Win7, both (double-clicking, prompt) have %SESSIONNAME% value "Console".
    – Stan
    Commented Jul 22, 2011 at 21:36
  • 1
    Doesn't work for me. SESSIONNAME always contains Console regardless on how I start the batch file (using Windows XP)
    – user330315
    Commented Jul 22, 2011 at 21:37
  • Odd, on my Win7 64bit, sessionname is only present when I run the script from the prompt directly.
    – Marc B
    Commented Jul 22, 2011 at 21:39
0

Use the tty command.

Use the -s option and check the return value.

Not the answer you're looking for? Browse other questions tagged or ask your own question.