3

This question seems to be (very) stupid be I can't deal with it :(

When I tried this batch code:

if "%1" == "-i" (
set is = %2
echo. %is%
shift
)

called with 2 (or more) arguments, it does NOT work. It actually prints a blank. The "shift" command is not done either. When I watch the executed code (without the @echo off at the beginning), I can see that the "set" command is completed.

What's wrong with it?

Example of calling:

c:\script.bat -i test -d bla
el_grom
  • 163
  • 1
  • 3
  • 15

1 Answers1

8

You have two issues. By default group of statements in parens will have variable expansion done all at once, that is before your set command. Also the semantics for set is wrong, you don't want spaces around the =.

Add this to the top of your file:

setlocal ENABLEDELAYEDEXPANSION

and remove the spaces around = in set:

set is=%2

Finally used delayed expansion:

echo. !is!

A possible third issue is you may need two SHIFTs, one for -i, one for it's is argument.

Update

Thanks to @dbenham for pointing out that it wasn't a syntax error with set, it's just surprising behavior that deserves a little explanation. If you execute these commands:

set a=one
echo "%a%"

The result is:

"one"

That makes sense, but try:

set b = two
echo "%b%"

And you get:

"%b%"

What? This is what you would expect when environment var b is unset. But we just set it. Or did we:

echo "%b %"

Displays:

" two"

For the Windows set command, unlike any other language or environment I'm aware of, the spaces are significant. The spaces before the = become part of the environment var name, spaces after become part of the value. This uncommon behavior is a common source of errors when writing Windows batch programs.

jimhark
  • 4,938
  • 2
  • 27
  • 28
  • I upvoted, but technically the OP's SET syntax is not wrong, just not what is wanted :-) The OP's code will create a variable with a trailing space in the name and a leading space in the value. – dbenham Dec 10 '12 at 11:32
  • @dbenham, I didn't say it was a syntax error. It's apparently a logic error because he later references the var without the space. – jimhark Dec 10 '12 at 11:34
  • Your 3rd sentence: *"Also your syntax for set is wrong..."* Relax, I upvoted because I think your answer is good. I'm just being pedantic. – dbenham Dec 10 '12 at 11:55
  • Oops, you're correct. I'll fix it when I'm at a keyboard (I'm mobile now). Thanks. – jimhark Dec 10 '12 at 12:01
  • @dbenham, fixed (and added section describing `set` behavior). Thanks for the correction. Sorry I didn't understand the first time. – jimhark Dec 10 '12 at 20:13
  • @el_grom, on my system I seem to need to use `!` to get delayed variable expansion, not `%`. Make sure your not picking up the value of `%is%` from a previous test run (make sure it's value is different and correct). – jimhark Dec 10 '12 at 20:17
  • Thanks for the 'set' explanation and the tip! I'll check that right now. I didn't know that 'set' was so "sensitive"... Good to know! Thanks :) – el_grom Dec 11 '12 at 08:02