windowscmd

Detect if bat file is running via double click or from cmd window


I have a bat file that does a bunch of things and closes the cmd window which is fine when user double clicks the bat file from explorer. But if I run the bat file from a already open cmd window as in cmd>c:\myfile.bat then I do not want the bat file to close the cmd window (END) since I need to do other things. I need bat dos command code that will do something like

if (initiated_from_explorer) then
else
endif

Is this possible ? thanks


Solution

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

    Personally, I would go for the %cmdcmdline% approach (not %O), but be aware that both start commands can be overridden in the registry…