1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to? Correctly use defined test

Discussion in 'Support' started by jmcm, Jan 4, 2012.

  1. jmcm

    Joined:
    Jun 11, 2008
    Messages:
    26
    Likes Received:
    0
    Hi

    I recently tried to (unsuccessfully) to use "defined" like this:
    Code:
    setlocal
    set var=%1
    
    iff defined %var then
    echo var defined as %var
    else
    echo var not defined
    endiff
    
    echo.
    echo end defined test
    
    This code always returns "var not defined". My expectation was that it should behave the same as if I had used "iff !%var! != !! then". What have I misunderstood about using defined?

    John
     
  2. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,869
    Likes Received:
    83
    Don't use the % in front of the variable name. (You were testing whether the contents of %var was an environment variable, not whether "var" was a defined variable.)
     
  3. jmcm

    Joined:
    Jun 11, 2008
    Messages:
    26
    Likes Received:
    0
    Thanks Rex, "var" is name of variable "%var" is content of var.

    John
     

Share This Page