Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to print elements of an array in batch script:

for %%i in (*.txt) do (
echo  !array[%n1%]!
set /A n1+=1

But it does not print the array elements. It prints the same thing again and again. I have also tried !array[!n1!]! but it does not work too.

share|improve this question
up vote 1 down vote accepted

Normal %n1% expansion occurs when the line is parsed. Any parenthesized block of code like your DO() clause is parsed all at once. So the value you see each iteration is the value that existed before the loop runs.

The wmz suggestion to use CALL works, but as he says, it is slow. There is a better way. You can use delayed expansion to transfer the n1 value to a FOR variable and then use the FOR variable as your "array" index.

If you are getting a syntax error with your REN command, it is probably because a file name contains a space, so it needs to be enclosed in quotes.

for %%F in (*.txt) do (
  for %%N in ("!n1!") do ren "%%F" "!array[%%N]!"
  set /a n1+=1

Note that ! is legal in file names. Any file name that contains ! will be corrupted when "%%F" is expanded because of the delayed expansion. The solution is to toggle delayed expansion on and off as needed within the loop.

setlocal disableDelayedExpansion
for %%F in (*.txt) do (
  set "name=%%F"
  setlocal enableDelayedExpansion
  for %%N in ("!n1!") do ren "!name!" "!array[%%N]!"
  set /a n1+=1
share|improve this answer
I stand corrected. Yet another way for usage :-) – wmz Dec 1 '12 at 17:12
Thanks a lot. THat seems to work. Forgive me if I seems stupid but what does for %%N in ("!n1!") do? DOes it increment %%N. If so how many times does it do it? – Sab Dec 2 '12 at 2:45
No, set /a n1+=1 increments the variable. The FOR statement is just transferring the current value of !n1! to %%N. It is an important trick to allow you 2 levels of expansion within a parenthesized block. In the expression !array[%%N]!, first %%N is expanded, to let us say 1. Then !array[1]! is expanded. – dbenham Dec 2 '12 at 4:01
Why cant we just transfer the value directly. Why doesnt that work? – Sab Dec 2 '12 at 13:41

Because of the way interpreter works, it's not as straightforward as it should. Most compact way is:
call echo %%array[!n1!]%% (this assumes setlocal enabledelayedexpansion)

Please note that call has significant overhead; and that interpreted does not really support arrays, it's just a clever way of naming variables so they look like an array.

share|improve this answer
That works , but how do I use it to rename files . When I say call ren %%i %%array[!n1!]%% it says wrong syntax. Whats the right way to use array elements to rename files? – Sab Dec 1 '12 at 14:52
@Sab Have a look at dbenham suggestion, it's (space) probably the cause – wmz Dec 1 '12 at 16:56
Yep. Space was the troublesome character. Also the for %%N in ("!n1!") do ren "%%F" "!array[%%N]!" really helped. – Sab Dec 2 '12 at 2:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .