1

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?

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.

| improve this answer | |
  • That's far from optimal, but if MS doesn't make the info available, it may be what we have to go with. – Jeff Dege Mar 7 '18 at 22:17
0

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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