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

前端 未结 12 1721
醉话见心
醉话见心 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

    after reading through the suggestions, this is what I went with:

    set __cmdcmdline=%cmdcmdline%
    set __cmdcmdline=%__cmdcmdline:"=%
    set __cmdcmdline=%__cmdcmdline: =%
    set __cmdcmdline=%__cmdcmdline:~0,5%
    if "%__cmdcmdline%"=="cmd/c" set CMD_INITIATED_FROM_EXPLORER=1
    set __cmdcmdline=
    

    which conditionally sets the variable: CMD_INITIATED_FROM_EXPLORER

    ..and can subsequently be used as needed:

    if defined CMD_INITIATED_FROM_EXPLORER (
      echo.
      pause
    )
    

    ..but the issue regarding Powershell that @Ruben Bartelink mentions isn't solved:

    running ./batch.cmd from Powershell uses cmd /c under the hood

提交回复
热议问题