3

I have a batch file that I'd like to be able to be run by double-clicking on the file in Windows Explorer. When this is done, I want to end with a PAUSE so the window doesn't immediately close.

But if the batch file is run from a command shell, I'd prefer to not end with a PAUSE.

Is there some way to tell, within a batch file, whether it is running in a command-line spawned from Windows Explorer, or from an existing command shell?

Bash provides the special $- environment variable.

Is there something similar in cmd.exe?

3

5 Answers 5

0

Not an exact solution, but you can create a shortcut to your cmd file and add a command-line parameter to the target. When you need to run your cmd from Explorer, you will have to start she shortcut, not the cmd file. In your cmd file you will test %1 parameter to identify whether it started from shortcut (from explorer) or from command prompt.

1
  • That's far from optimal, but if MS doesn't make the info available, it may be what we have to go with. Commented Mar 7, 2018 at 22:17
3

I tried Gene's suggestion on Windows 10:

if /I Not "%CMDCMDLINE:"=%" == "%COMSPEC% " Pause 

But that didn't work for me. HOWEVER, when I removed the space at the end of %COMSPEC%, it did work:

if /I Not "%CMDCMDLINE:"=%" == "%COMSPEC%" Pause 

I don't have the reputation to just comment on the original post, but would prefer someone add this to Gene's answer and upvote his (since he gave me the basis by which to solve my problem).

3
  • I'm not sure why, but sometimes there is a space after %CMDCMDLINE%, sometimes there is not echo if /I "%CMDCMDLINE:"=%" NEQ "%COMSPEC%" ( echo yes ) else ( echo no ) becomes if /I "C:\Windows\system32\cmd.exe " NEQ "C:\Windows\system32\cmd.exe" ( echo yes ) else ( echo no ) Commented Feb 27, 2024 at 0:17
  • ::Usage Call :IsConsole && echo Is Console || echo Is NOT console :IsConsole if /I "%CMDCMDLINE:"=%" EQU "%COMSPEC% " exit /b 0 if /I "%CMDCMDLINE:"=%" EQU "%COMSPEC%" ( exit /b 0 ) else ( exit /b 1 ) Commented Feb 27, 2024 at 0:22
  • This works in both cases Commented Feb 27, 2024 at 0:22
1

I posted a solution to a similar question here: 886848/how-to-make-windows-batch-file-pause-when-double-clicked.

That solution should work for you, but it's is a bit more involved than you probably need.

I posted a shortened version of it below which is simpler and should work for you.

You can read more about it at the solution linked above, but the basics of it is that it uses the environment variable: %cmdcmdline% to determine if the batch file was run from a command window or not.

It works because the contents of the variable %cmdcmdline% are different depending on how the batch file was started: 1) By clicking on a batch file or shortcut, like from Windows Explorer or on the Desktop, or 2) Running the batch file from within a Command Prompt window.

So, you use it like this:

At the point where the batch file will exit, you add some code like this:

set newcmdcmdline=%cmdcmdline:"=-% echo %newcmdcmdline% | find /i "cmd /c --%~dpf0%-" set "result=%errorlevel%" rem if %result% EQU 0 rem this batch file was executed by clicking a batch file rem or a shortcut to a batch file, typically from Windows Explorer rem or the Desktop, or the "Start Menu" ... rem if %result% NEQ 0 rem this batch file was executed from within a Command Prompt rem if executed from within a Command Prompt: rem go exit the batch script immediately, without pausing. rem since this batch file was executed from within a rem Command Prompt, the command window will remain open. rem You can use either of these to exit the batch file: rem goto :EOF rem exit /b if %result% NEQ 0 goto :EOF rem at this point, we know this batch file was executed by clicking ..., rem NOT from within a Command Prompt. rem leave the command prompt window open to allow the user time to rem view the messages on the screen before exiting. Use any of rem these to pause and or interact with the user before exiting: rem pause rem echo Message... &pause rem set /p "d=Message..." rem choice [Options...] rem (choice /? Displays help message for the Choice command) rem timeout /T wait-time [Options...] rem (timeout /? Displays help message for the Timeout command) timeout /t 10 goto :EOF 
1

The shortest solution which I know works on Windows 10 (I don't have XP to test it on) is one of the following one-liners:

  1. If /I Not "%CMDCMDLINE:"=%" == "%COMSPEC% " "%SystemRoot%\System32\timeout.exe" /T 60

  2. If /I Not "%CMDCMDLINE:"=%" == "%COMSPEC% " Pause

Note: The space after %COMPSPEC% is critical because Windows always places a space at the end of %CMDCMDLINE%

0

None of the given answers worked reliable for me in all cases, especially when launched from desktop via windows shortcut or from a 3rd party file manager, e.g. FreeCommander

What works for me is this:

if "%CMDCMDLINE:"=%" == "%COMSPEC%" ( REM started via windows explorer ) else if "%CMDCMDLINE%" == "cmd.exe" ( REM started via file manager (e.g. FreeCommander) ) else if "%CMDCMDLINE:"=%" == "%COMSPEC% /c %~dpf0 " ( REM started in command window ) else ( REM started from other batch file ) 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.