Detect if batchfile is launched from explorer or dosprompt

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mich
Posts: 8
Joined: 22 Sep 2011 00:19

Detect if batchfile is launched from explorer or dosprompt

#1 Post by mich » 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

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Detect if batchfile is launched from explorer or dosprom

#2 Post by dbenham » 02 Jan 2012 11:15

Looks like a good, practical solution to me.

I can confirm it works fine on Vista.

Dave Benham

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Detect if batchfile is launched from explorer or dosprom

#3 Post by Squashman » 02 Jan 2012 12:06

That is a cool idea. Most of my batch files are normally launched from explorer so that will come in handy.
Last edited by Squashman on 02 Jan 2012 12:50, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Detect if batchfile is launched from explorer or dosprom

#4 Post by aGerman » 02 Jan 2012 12:27

Sorry to belie, but there is no safe way to determine from where the batchfile is launched.

CMD Prompt:

Code: Select all

cmd /c test.bat

Result:
Launched via explorer or shortcut


You need to figure out the parent process but this cannot be done with batch.

Regards
aGerman

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Detect if batchfile is launched from explorer or dosprom

#5 Post by dbenham » 02 Jan 2012 13:12

Sure - the technique is far from fool proof. But I think it will give the correct result for most "normal" circumstances that mich is likely to run into. And the repercussions for getting the answer wrong are not dire in his case.

Dave Benham

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Detect if batchfile is launched from explorer or dosprom

#6 Post by aGerman » 02 Jan 2012 16:05

You're probably right, Dave.

I have to however correct myself. There is a possibility to determine the parent process name using WMIC (if available).

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set "myName=%~nx0"
set "pPID="
set "pName="

setlocal EnableDelayedExpansion
for /f "skip=1" %%i in (
  'WMIC PATH Win32_Process WHERE "Name='cmd.exe' AND CommandLine LIKE '%%!myName!%%' AND NOT CommandLine LIKE '%%WMIC PATH Win32_Process WHERE%%'" GET ParentProcessId'
) do if not defined pPID set /a "pPID=%%i" 2>nul
if not defined pPID (
  echo CMD Prompt Supposed
) else (
  for /f "skip=1 delims=" %%i in (
    'WMIC PATH Win32_Process WHERE "ProcessID='%pPID%'" GET Name 2^>nul^|findstr .'
  ) do if not defined pName set "pName=%%i"
  if defined pName (
    echo Parent Process Name: !pName!
  ) else (
    echo Parent Process Was Already Terminated.
  )
)
pause

Regards
aGerman

mich
Posts: 8
Joined: 22 Sep 2011 00:19

Re: Detect if batchfile is launched from explorer or dosprom

#7 Post by mich » 03 Jan 2012 01:22

Thank you very much for all replies.

My motivation for this batch is sheer laziness. I don't want to hit any key to end the script when I run it from the prompt while still adding the pause statement for my colleagues running it from a short-cut.

@aGerman

I will be chewing on the code you provided to see if I can understand how it works :-) Taking things to a different level. I was not even aware of this wmic command.

Cheers,

Mich

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Detect if batchfile is launched from explorer or dosprom

#8 Post by orange_batch » 03 Jan 2012 04:02

Just my 2 cents, WMI is very useful. As aGerman said, the command line WMIC tool isn't available with all relevant versions of Windows though. Accessing WMI through VBScript is, however. Basic VBScript WMI construct:

Code: Select all

set a=getobject("winmgmts:")
set b=a.execquery("select whatever_property from win32_whatever_resource")
for each c in b
wscript.echo c.whatever_property
next

http://msdn.microsoft.com/en-us/library ... 85%29.aspx

You can retrieve things like the CMD line with WMI easily as well. (win32_process)

Post Reply