2

I am trying to display a list of variables in Batch, however I don't get the variables back. I get the command back but stripped of % and spaces.

echo 1 %11A1% %11B1% %11C1% %11D1% %11E1% %11F1% %11G1% %11H1% %11I1% %11J1%

turns into

1 1A111B111C111D111E111F111G111H111I111J1

I have done variables in other batches and displayed them similarly without problem. Any help is appreciated!

Current code here: http://hastebin.com/sutasipiju.dos

2 Answers 2

6

You have a problem with the naming of your variables. As they start with a number, the fisrt character in name of variable preceded by a percent sign is being considered as a parameter. So, %11A1% (and in the rest of variables) are getting translated to %1 1A1 ...

From command line this works without problems as there are no parameter substitution, but in batch files variables starting with a number generate this kind of problems.

There are two solutions to this.

  • Rename all the variables to something that not start with a number
  • Enable delayed expansion ('setlocal enabledelayedexpansion') and change the sintax from %11A1% to !11A1!

First option is probably better as is simpler and can generate less problems, but both can make it work

Sign up to request clarification or add additional context in comments.

Comments

5

The problem is with your variablenames, which should not start with a numeric.

When batch sees echo %11A1%...

it substitutes the first command-line parameter for the %1, hence, assuming you have provided no parameters, this will become

[nothing]1A1...

Solution : start your variables with some other character. "A..Z" are traditional. If you want to be a little exotic, then $ and # seem obvious choices.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.