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

前端 未结 12 1654
醉话见心
醉话见心 2020-12-29 02:54

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 al

12条回答
  •  离开以前
    2020-12-29 03:37

    %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 this var is cmd /c ""{full_path_to_the_bat_file}" ";
      this implicates that you might also check the %0 variable in your bat file, for in this case it is always the full path to the bat file, and always enclosed in double quotes.

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

提交回复
热议问题