Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out if a batch file was opened by double click, or by command line

On a Windows 7, I have an executable, say immutableProg.exe, which I want to call 3 times with certain parameters. This is done by the batch file myBatch.bat.

Content of myBatch.bat:

immutableProg.exe -a
immutableProg.exe -b
immutableProg.exe -c

The executable immutableProg.exe does have a special --keep switch which stops the executable from returning until a user hits any key. Now I want to add the --keep switch if and only if my batch myBatch.bat got double clicked like:

immutableProg.exe -a
immutableProg.exe -b
immutableProg.exe -c --keep

It shall NOT be added if a user calls the batch from commandline.

The question: How can I find out (from inside my batch's view) if it was opened by a double click or from command line?

Changing the default behavior of the immutableProg.exe is unfortunatelly not an option, neither is to give the batch file an extra parameter from commandline.

like image 288
McK Avatar asked Apr 23 '14 15:04

McK


People also ask

What does == mean in batch file?

[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.

What does @echo off do in a batch file?

To prevent echoing a particular command in a batch file, insert an @ sign in front of the command. To prevent echoing all commands in a batch file, include the echo off command at the beginning of the file.

What does %1 do in batch?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

What does && do in batch file?

&& runs the second command on the line when the first command comes back successfully (i.e. errorlevel == 0 ). The opposite of && is || , which runs the second command when the first command is unsuccessful (i.e. errorlevel != 0 ).


2 Answers

%cmdcmdline% gives the exact command line used to start the current Cmd.exe.

When launched from a command console, this var is "%SystemRoot%\system32\cmd.exe".

When launched from explorer (double clicked) this var is cmd /c ""{full_path_to_the_bat_file}"

like image 158
haxtbh Avatar answered Nov 15 '22 07:11

haxtbh


To actually use the info in haxtbh's answer, you can do the following. It is not fool proof, but it usually works fine. It would take an unusual scenario for it to give a false reading.

echo %cmdcmdline%|find /i """%~f0""">nul && echo doubleClick || echo console launch
like image 38
dbenham Avatar answered Nov 15 '22 06:11

dbenham