9
set var=%1
echo var
set command=""
IF var==true ( 
    set command=dir
)

IF var==false ( 
    set command=dir /a
)
echo %command%
%command%

So, if I run this script by typing in

C:\>test true

the echo %command% always prints "". Any idea?

Joey
  • 344,408
  • 85
  • 689
  • 683
zs2020
  • 53,766
  • 29
  • 154
  • 219

2 Answers2

15

You are missing some % signs that are needed for any variable dereferencing.

You probably want it like this:

set var=%1
echo %var%
set command=""
IF %var%==true (                    <=== here
    set command=dir
)

IF %var%==false (                   <=== here
    set command=dir /a
)
echo %command%
%command%

You should also surround your string comparisons with quotes to allow for spaces, something like this:

IF "%var%"=="false"

Also, as Joey pointed out, clearing a variable is done by

set command=

without any quotes. Otherwise you will initialize the the variable with these quotes, leading to your weird output.

Frank Bollack
  • 24,478
  • 5
  • 49
  • 58
  • That's not the place where they indicated a problem. The problem from the question persists. – Joey Jun 24 '11 at 20:35
  • @Joey: I think you are wrong there. Because `var` is never referenced correctly, `command` is never initialised with the expected value. – Andriy M Jun 24 '11 at 20:36
  • @Joey: not for me. Are you calling the script with either `true` or `false` as parameter? – Frank Bollack Jun 24 '11 at 20:38
  • I can confirm the problem from Joey still persists with correct %% quuotation in my case on Win7 64bit but it's not reproducable by the script above .. i'll check if I can get another example. Really strange .. – childno͡.de Nov 27 '14 at 12:45
  • `set command=` with no quotes and no space seems to wrap around to the next line and overall does very odd things to the script. I've spent ages debugging it! – Violet Giraffe Apr 14 '21 at 16:13
7

I digged out that variable is empty if you are using conditions and set in conditions!

if errorlevel 0 (
   Set localVar="fooBar"
   echo "%localVar%"
)

will result in an empty output!

related to string comparison in batch file use

SetLocal EnableDelayedExpansion

to enable !VARNAME! that will enable to use !VARNAME! in a condition beneath but will still not enable output for %VARNAME% in the conditional block!

Use Set BEFORE the condition to get it accessible in the conditional block. OR usage must be AFTER conditional block where Set was used in.

(!) Currently I don't know how to challenge Set AND usage in the same block!

See some example code in https://gist.github.com/childnode/0f6c874ad79788a86332

(!) But as you can see in the results (also in gist) using DelayedExpansion has a different side effect:

Variable is set on second run in same shell (what is obviously correct) but for some reasons are not set with EnableDelayedExpansion in second run (seems to it also cleans up "local" variables from script and doesn't export it for following commands! This might cause other error if different batch files are run in "pipe"

Community
  • 1
  • 1
childno͡.de
  • 4,679
  • 4
  • 31
  • 57