Thursday, January 20, 2011

Checking parameters in a DOS batch file

We have a batch file that takes parameters.

We then read the value of the parameter using %1 for the first parameter.

Question is: How can we check that %1 has a value?

  • According to http://www.robvanderwoude.com/parameters.php you can check them with an if:

    • IF "%1"=="" for non-quoted parameters

    or

    • IF [%1]==[]

    or

    • IF "%~1"=="" (only NT4+SP6 and later)
  • You can branch on the value of %1. For example, one way to do this is

    if "%1"=="" goto bad
    
    :good
    
    rem Do processing here
    
    goto end
    
    :bad
    
    rem Do error handling here
    
    :end
    
    grawity : It's `%1`, not `%1%`
    dsolimano : Good catch, fixed
    From dsolimano
  • you can try this

    Set test=%1

    if %test%.==. (echo test not set)

    From gaotter

0 comments:

Post a Comment