0

I am new to bat. My question is why this block of code does not print something like "name is 0001".

for /l %%i in (1,1,124) do (
    set aaa=000%%i
    echo name is %aaa%
)

It only gives me back as name is.

So can anyone explain how this happens? Thanks a lot!

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Boooooooooms
  • 306
  • 4
  • 21
  • What about `call echo name is %^aaa%`? – double-beep Mar 02 '19 at 10:55
  • 1
    As an example of the `Call` method, try this as a single line batch file, `@(For /L %%A In (1 1 124)Do @Set "aaa=000%%A"&Call Echo(name is %%aaa:~-3%%)&Pause` – Compo Mar 02 '19 at 11:09
  • @Compo Thx for your help! I am not clear what the `@Set` and `@Call` means. Can you type this in the answer so it can be displayed in a more clearly way? Again, thx a lot. – Boooooooooms Mar 02 '19 at 11:12
  • 1
    @Boooooooooms `@` is to inhibit the commands themselves from showing on the cmd/batch window. As for other commands, you can open `cmd` first, and type `command /?` to read their manual, for example `set /?` and `call /?`. – Til Mar 02 '19 at 11:15
  • 1
    @Tiw, thx! Got it! – Boooooooooms Mar 02 '19 at 11:25
  • @Boooooooooms, there is no `@Call`! and all you need to do, is to open up notepad, paste in those `82` characters, and save as > all files > `example.cmd`. Then double-click on the file to run it and see it in action! – Compo Mar 02 '19 at 11:25
  • @Compo, I got it! Really cool codes! Character as `&` and `@` are still new to me. – Boooooooooms Mar 02 '19 at 11:27
  • 1
    @Boooooooooms: You should know that the _standard way_ to solve this problem is via Delayed Expansion as described in the linked answer (and a ton more answers about the same topic): insert `setlocal EnableDelayedExpansion` at beginning and use `echo name is !aaa!`. The `call` method is an unintended and officialy undocumented behavior ("trick") that works by chance... It is also much slower than delayed expansion. – Aacini Mar 02 '19 at 12:34

0 Answers0