detect batch launching method (prompt or icon double-click)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Pilou
Posts: 6
Joined: 07 Jan 2014 08:42

detect batch launching method (prompt or icon double-click)

#1 Post by Pilou » 07 Jan 2014 08:52

Hi everyone & thanks for this great and so useful DOS site :!:

I have a simple but unfortunately unanswered question : is there a way to detect whether a batch script is launched manually from a command prompt or by icon double-clicking ? The goal is to add a pause only with the second way...

Thanks for your responses :)
Last edited by Pilou on 07 Jan 2014 10:22, edited 1 time in total.


Pilou
Posts: 6
Joined: 07 Jan 2014 08:42

Re: detect batch launching method (prompt or icon double-cli

#3 Post by Pilou » 07 Jan 2014 13:40

Great, thx a lot :D

With Win7 I had to adapt a little bit, which gives :
if "%CMDCMDLINE:~1,2%" == "md" pause

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: detect batch launching method (prompt or icon double-cli

#4 Post by AiroNG » 07 Jan 2014 16:27

Out of curiosity i fiddled around a bit with "echo %cmdcmdline%" and got some strange results...

Code: Select all

D:\>echo %cmdcmdline%
cmd

D:\>echo %cmdcmdline:~0,3%
cmd

D:\>echo %cmdcmdline:~1,3%
md

D:\>echo %cmdcmdline:~2,3%
ECHO ist eingeschaltet (ON).

D:\>echo %cmdcmdline:~1,3%
ECHO ist eingeschaltet (ON).

D:\>echo %cmdcmdline:~0,3%
ECHO ist eingeschaltet (ON).

Then i tried the same thing with Pilou's case:

Code: Select all

D:\>echo %cmdcmdline:~0,2%
cm

D:\>echo %cmdcmdline:~1,2%
m

D:\>echo %cmdcmdline:~1,2%
ECHO ist eingeschaltet (ON).

D:\>echo %cmdcmdline:~0,2%
ECHO ist eingeschaltet (ON).

now for the really strange part:

Code: Select all

D:\>echo %cmdcmdline:~1,2%
md

D:\>echo %cmdcmdline:~1,2%
d

D:\>echo %cmdcmdline:~1,2%
ECHO ist eingeschaltet (ON).


Does this mean the variable %cmdcmdline% is changed by merely echoing it?

Can someone explain this behaviour?

ps.: tests where conducted with Windows 7 - 64bit

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

Re: detect batch launching method (prompt or icon double-cli

#5 Post by dbenham » 07 Jan 2014 17:00

OMG :shock: :?

That is just so wrong.

I can only assume it is a nasty bug.

Workaround - assign the complete value to a normal variable and then take the substring of the copy.


Dave Benham

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: detect batch launching method (prompt or icon double-cli

#6 Post by penpen » 07 Jan 2014 18:32

Actually i'm surfing on my smartphone, so:
Has anyone tried the above with other dynamic variables, such as %CD%, ..., %TIME%?
(Maybe i try it out tomorrow.)

penpen

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

Re: detect batch launching method (prompt or icon double-cli

#7 Post by dbenham » 07 Jan 2014 18:43

penpen wrote:Actually i'm surfing on my smartphone, so:
Has anyone tried the above with other dynamic variables, such as %CD%, ..., %TIME%?
(Maybe i try it out tomorrow.)

penpen

I tried both %CD:~1,2% and %TIME:~1,2%, and nothing out of the ordinary.


Dave Benham

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: detect batch launching method (prompt or icon double-cli

#8 Post by AiroNG » 07 Jan 2014 19:47

It is also present in winXP - 32bit:

Code: Select all

C:\>echo %cmdcmdline%
"C:\WINDOWS\system32\cmd.exe"

C:\>echo %cmdcmdline:~0,3%
"C:

C:\>echo %cmdcmdline:~1,3%
C:

C:\>echo %cmdcmdline:~1,3%
:

C:\>echo %cmdcmdline:~1,3%
ECHO ist eingeschaltet (ON).

I tested other dynamic variables and got the same results as dbenham.

edit: Sorry for "hijacking" this topic, Pilou :(

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: detect batch launching method (prompt or icon double-cli

#9 Post by AiroNG » 08 Jan 2014 13:47

dbenham wrote:OMG :shock: :?

That is just so wrong.

I can only assume it is a nasty bug.

Workaround - assign the complete value to a normal variable and then take the substring of the copy.


Dave Benham


One can do this also:

Code: Select all

D:\>set "cmdcmdline=%cmdcmdline%"

D:\>echo: %cmdcmdline%
 cmd

D:\>echo: %cmdcmdline:~1,2%
 md

D:\>echo: %cmdcmdline:~1,2%
 md

D:\>echo: %cmdcmdline:~1,2%
 md

D:\>echo: %cmdcmdline%
 cmd


Either it is, as you've said, a nasty bug or it serves as the best proof to "don't do drugs and code" ;)

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

Re: detect batch launching method (prompt or icon double-cli

#10 Post by Squashman » 08 Jan 2014 14:49

AiroNG wrote:Either it is, as you've said, a nasty bug or it serves as the best proof to "don't do drugs and code" ;)

80's Flashback
I want a new drug
One that won't make me sick
One that won't make me crash my CODE
Or make me feel three feet thick

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: detect batch launching method (prompt or icon double-click)

#11 Post by siberia-man » 03 Mar 2018 03:02

Hello guys,

I know this is too old topic and there is no robust solutions. I remember that it has been discussed here but haven't found any methods mentioning the similar way. Anyway I'd like to share some finding with you.

Googling one question that not having to do with the current topic I payed my attention on the link to this page http://steve-jansen.github.io/guides/wi ... ricks.html. At the end of this article the author shortly discusses the way how to detect if some script is launched by double click in Explorer. I performed some tests and extended his solution.

Method coverage
-- double click in Explorer
-- direct launch of the script from "Start/Run..." menu

Code: Select all

@echo off

echo:%CMDCMDLINE%
echo:%COMSPEC%

echo:%CMDCMDLINE%|findstr /b /l /c:"%COMSPEC% /c" && (
	echo:
	echo:Explorer Launcher
	echo:
	pause
)

pause
When the script is launched via the "Start/Run..." menu or double clicking the script in Explorer, the beginning of the %CMDCMDLINE% exactly matches the string %COMSPEC% /c with the only space between the command and the option. The rest of the invoking string doesn't matter. Any other invocation of batch files have double space before the /c option and could have double quotes surrounding the cmd.exe.

Unfortunately there is no reliable way to determine the script execution from "Start/Run" menu in the forms as below, because
-- a user could put more than one spaces in the invoking string before /c option
-- the full path to cmd.exe could be surrounded with double quotes

Code: Select all

cmd /c batchfile
fullpath\to\cmd.exe /c batchfile
%comspec% /c batchfile

Pyprohly
Posts: 3
Joined: 24 Dec 2014 22:48

Re: detect batch launching method (prompt or icon double-click)

#12 Post by Pyprohly » 03 Mar 2018 07:46

Did you see the thread Squashman linked to? The WMIC calls are a little slow but aGerman’s idea of checking the parent process seems pretty robust.

Code: Select all

@echo off

goto :main

:get_pid [OutVar]
setlocal
	for /f "skip=1" %%I in ('
		wmic process Where "Name='cmd.exe'" Get ParentProcessId
	') do for %%J in (%%I) do set "pid=%%J"

	for /f %%I in ('
		2^>nul wmic process Where "ProcessId='%pid%' And (CommandLine Like '%%%~nx0%%')" Get ParentProcessId
	') do for %%J in (%%I) do set "pid=%%J"
endlocal & if "%~1"=="" (echo(%pid%) else set "%~1=%pid%"
goto :eof

:get_execution_context
setlocal
	call :get_pid pid

	for /f "skip=1" %%I in ('
		2^>nul wmic process Where "(Name='cmd.exe' OR Name='powershell.exe') And ProcessId='%pid%'" Get ParentProcessId
	') do for %%J in (%%I) do set "parent_pid=%%J"
endlocal & (
	if "%parent_pid%"=="" (
		set execution_context=0
	) else (
		set execution_context=1
	)
)
goto :eof

:main
call :get_execution_context

if %execution_context% equ 0 (
	echo Explorer context
	pause
) else (
	echo Command-line context
)

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: detect batch launching method (prompt or icon double-click)

#13 Post by siberia-man » 03 Mar 2018 13:38

Yes. I've seen it. But my intention was to share simple way even it is not 100% reliable.

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: detect batch launching method (prompt or icon double-click)

#14 Post by pieh-ejdsch » 04 Mar 2018 15:44

you can use the sub :checkcmdline
viewtopic.php?p=54962#p54962
this go in a for /f loop and piped batch.
https://www.administrator.de/wissen/bat ... 84736.html

Post Reply