Detect if batchfile is launched from explorer or dosprompt
Posted: 02 Jan 2012 02:42
Best Wishes for 2012 to you all.
I'm posting this here because I already got a lot of useful information from this site. This however, I found somewhere, else and thought other people may be looking for a similar solution.
I've created some batch-files that display various types of info and received the feedback that the screen closes to quick to see the batch output. I found my colleague's had created short-cuts to launch the batch-files rather then running them from the prompt.
Rather then adding a PAUSE statement to every batch I felt it would be more elegant to add the statement when the script was not launched from the prompt. So I added a few lines that do just that.
If anyone feels it could be done more elegant or more OS version proof, please comment.
Best regards,
Michel
Of course the minimal version I added to the batch files looks like this:
I'm posting this here because I already got a lot of useful information from this site. This however, I found somewhere, else and thought other people may be looking for a similar solution.
I've created some batch-files that display various types of info and received the feedback that the screen closes to quick to see the batch output. I found my colleague's had created short-cuts to launch the batch-files rather then running them from the prompt.
Rather then adding a PAUSE statement to every batch I felt it would be more elegant to add the statement when the script was not launched from the prompt. So I added a few lines that do just that.
If anyone feels it could be done more elegant or more OS version proof, please comment.
Best regards,
Michel
Code: Select all
@echo off
setlocal enabledelayedexpansion
REM
REM The variable CMDCMDLINE has a different value when launched from a command prompt or from an explorer
REM C:\Windows\system32\cmd.exe Launched from dosbox
REM cmd /c ""C:\T\test\detect.bat" " Launched via explorer or shortcut
REM Tested on win7 only, so you may want to add ECHO %cmdcmdline% to test your version
REM
set b=%cmdcmdline:~0,3%
IF NOT "%b%" == "cmd" goto:dos
IF "%b%" == "cmd" PAUSE
:exp
REM Do stuff you want to do in explorer or shortcut launch
REM for example PAUSE
ECHO Launched via explorer or shortcut
GOTO:EOF
:dos
REM Do stuff you want to do in dos box
ECHO Launched from dosbox
GOTO:EOF
Of course the minimal version I added to the batch files looks like this:
Code: Select all
set b=%cmdcmdline:~0,3%
IF "%b%" == "cmd" PAUSE