How can I check if an argument is defined when starting/calling a batch file?

后端 未结 7 1816
无人共我
无人共我 2020-12-13 03:21

I\'m trying to use the following validation logic in a batch file but the \"usage\" block never executes even when no parameter is supplied to the batch file.



        
7条回答
  •  悲哀的现实
    2020-12-13 03:45

    The check for whether a commandline argument has been set can be [%1]==[], but, as Dave Costa points out, "%1"=="" will also work.

    I also fixed a syntax error in the usage echo to escape the greater-than and less-than signs. In addition, the exit needs a /B argument otherwise CMD.exe will quit.

    @echo off
    
    if [%1]==[] goto usage
    @echo This should not execute
    @echo Done.
    goto :eof
    :usage
    @echo Usage: %0 ^
    exit /B 1
    

提交回复
热议问题