简体   繁体   中英

Issue displaying variables in Batch

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM