Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 2 down vote accepted

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
share|improve this answer
    
It's %1, not %1% –  grawity Mar 2 '10 at 16:36
    
Good catch, fixed –  dsolimano Mar 2 '10 at 16:59

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)
share|improve this answer

So here is my solution to this issue. I have used this style to use "Named Parameters" insted of the traditional positional values. That way, validation can be done on default and used parms.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: batchparms.bat = This is an example of named parameters in a batch file. ::
:: This demonstrates the ability to name parameters instead of the current  ::
:: positional restrictions provided natively.                   ::
::                                      ::
:: Parameters:                                  ::
:: -first   = First named parm. Should be AAA BBB or CCC [Default is AAA]   ::
:: -second  = Second named parm. Should be XXX YYY or ZZZ [Default is ZZZ]  ::
:: -flagone = First named switch [Default is TRUE]              ::
:: -flagtwo = Second named switch [Default is TRUE]             ::
::                                      ::
:: 02/19/2010   - HighDesertRaider                      ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::                                      ::
:: 02/22/2012   - Modified to be used as a Demo                 ::
::      - HighDesertRaider                      ::
::                                      ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Set Environment
::
:: These flags need to be set off to use named parameters
::
SET FLAG_FIRST=0
SET FLAG_SECOND=0
SET FLAG_ONE=0
SET FLAG_TWO=0
::
:: Set Default Values
::
CLS
SET PARM_FIRST=AAA
SET PARM_SECOND=ZZZ
SET PARM_ONE=1
SET PARM_TWO=1
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Parse the input parameters, and assign the arguments to variables.       ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
FOR %%A in (%*) DO (
IF !FLAG_FIRST!==1 (
  SET PARM_FIRST=%%A
  SET FLAG_FIRST=0
) ELSE (
IF !FLAG_SECOND!==1 (
  SET PARM_SECOND=%%A
  SET FLAG_SECOND=0
) ELSE (
IF /I %%A==-FIRST (
  SET FLAG_FIRST=1
) ELSE (
IF /I %%A==-SECOND (
  SET FLAG_SECOND=1
) ELSE (
IF /I %%A==-FLAGONE (
  SET PARM_ONE=0
) ELSE (
IF /I %%A==-FLAGTWO (
  SET PARM_TWO=0
) ELSE (
  SET BADPARMMSG=Parameter %%A is not a valid option.
  GOTO:BadFormExit)))))))
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Validate -ParmFirst                              ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ValidateParmFirst
ECHO  Validate -ParmFirst %PARM_FIRST%
FOR %%A in (AAA BBB CCC) DO (
    IF /I %PARM_FIRST%==%%A (
        GOTO ValidParmFirst))
::
:: A Valid Parm Value was not found in the list
::
SET BADPARMMSG=Value %PARM_FIRST% is not a valid value for -ParmFirst
ECHO Please select one of the following values:
ECHO AAA
ECHO BBB
ECHO CCC
GOTO BadFormExit
:ValidParmFirst
ECHO  Value %PARM_FIRST% is valid for -ParmFirst
ECHO.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Validate -ParmSecond                             ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ValidateParmSecond
ECHO  Validate -ParmSecond %PARM_SECOND%
FOR %%A in (XXX YYY ZZZ) DO (
    IF /I %PARM_SECOND%==%%A (
        GOTO ValidParmSecond))
::
:: A Valid Parm Value was not found in the list
::
SET BADPARMMSG=Value %PARM_FIRST% is not a valid value for -ParmSecond
ECHO Please select one of the following values:
ECHO XXX
ECHO YYY
ECHO ZZZ
GOTO BadFormExit
:ValidParmSecond
ECHO  Value %PARM_SECOND% is valid for -ParmSecond
ECHO.
::
::
::
ECHO.
ECHO The value of -first is %PARM_FIRST%
ECHO The value of -second is %PARM_SECOND%
ECHO The value of -flagone is %PARM_ONE%
ECHO The value of -flagtwo is %PARM_TWO%
ECHO.
GOTO:RunMyCodeCauseIGotGoodParms
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: This function shows the user the correct form to use             ::
::  and exits the script.                           ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:BadFormExit
ECHO.
ECHO  Sorry friend, but your form is bad.
ECHO.
ECHO  batchparms.bat %* is not correct.
ECHO  Reason: %BADPARMMSG%
ECHO. 
ECHO  the correct syntax for this command is:
ECHO "batchparms.bat [-first AAA | BBB | CCC] [-second XXX | YYY | ZZZ] [-flagone] [-flagtwo]"
EXIT /B 1
:RunMyCodeCauseIGotGoodParms
::
:: Insert Code Here to Run once Parms are Validated
::
EXIT /B

Parmameter values could be listed in a file, so that you don't have to change the batch file, just to add a new value. and more text could be added to explain values, etc.

share|improve this answer

you can try this

Set test=%1

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.