Page 1 of 1

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

Posted: 07 Jan 2014 08:52
by Pilou
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 :)

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

Posted: 07 Jan 2014 08:55
by Squashman

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

Posted: 07 Jan 2014 13:40
by Pilou
Great, thx a lot :D

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

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

Posted: 07 Jan 2014 16:27
by AiroNG
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

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

Posted: 07 Jan 2014 17:00
by dbenham
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

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

Posted: 07 Jan 2014 18:32
by penpen
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

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

Posted: 07 Jan 2014 18:43
by dbenham
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

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

Posted: 07 Jan 2014 19:47
by AiroNG
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 :(

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

Posted: 08 Jan 2014 13:47
by AiroNG
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" ;)

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

Posted: 08 Jan 2014 14:49
by Squashman
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

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

Posted: 03 Mar 2018 03:02
by siberia-man
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

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

Posted: 03 Mar 2018 07:46
by Pyprohly
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
)

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

Posted: 03 Mar 2018 13:38
by siberia-man
Yes. I've seen it. But my intention was to share simple way even it is not 100% reliable.

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

Posted: 04 Mar 2018 15:44
by pieh-ejdsch
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