Detecting how a batch file was executed

后端 未结 9 1816
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 08:24

Assuming Windows, is there a way I can detect from within a batch file if it was launched from an open command prompt or by double-clicking? I\'d like to add a pause to the

9条回答
  •  天涯浪人
    2020-12-28 09:08

    One easy way to do it is described here: http://steve-jansen.github.io/guides/windows-batch-scripting/part-10-advanced-tricks.html There is little typo in the code mentioned in the link. Here is correct code:

    @ECHO OFF
    SET interactive=0
    
    ECHO %CMDCMDLINE% | FINDSTR /L /I %COMSPEC% >NUL 2>&1
    IF %ERRORLEVEL%==0 SET interactive=1
    
    ECHO do work
    
    IF "%interactive%"==1 PAUSE
    EXIT /B 0
    

提交回复
热议问题