Page 1 of 1

Detect if batchfile is launched from explorer or dosprompt

Posted: 02 Jan 2012 02:42
by mich
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

Re: Detect if batchfile is launched from explorer or dosprom

Posted: 02 Jan 2012 11:15
by dbenham
Looks like a good, practical solution to me.

I can confirm it works fine on Vista.

Dave Benham

Re: Detect if batchfile is launched from explorer or dosprom

Posted: 02 Jan 2012 12:06
by Squashman
That is a cool idea. Most of my batch files are normally launched from explorer so that will come in handy.

Re: Detect if batchfile is launched from explorer or dosprom

Posted: 02 Jan 2012 12:27
by aGerman
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

Re: Detect if batchfile is launched from explorer or dosprom

Posted: 02 Jan 2012 13:12
by dbenham
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

Re: Detect if batchfile is launched from explorer or dosprom

Posted: 02 Jan 2012 16:05
by aGerman
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

Re: Detect if batchfile is launched from explorer or dosprom

Posted: 03 Jan 2012 01:22
by mich
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

Re: Detect if batchfile is launched from explorer or dosprom

Posted: 03 Jan 2012 04:02
by orange_batch
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)