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.
[ == ] (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.
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.
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.
&& 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 ).
%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}"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With