0

I am trying to write a batch file that reads each line in a text file and assigns it (each line) to a variable.

I am using the example found here: Read each line in a txt file and assign variables using windows dos commands, which is:

SETLOCAL EnableDelayedExpansion
SET count=1
FOR /F "tokens=* delims= usebackq" %%x IN ("%TEXT_T%") DO (
SET var!count!=%%x
SET /a count=!count!+1
)
ENDLOCAL

Now, all I want to do is echo the results of each variable, but I just can't seem to be able to do so. Below is what I have, which gives me the results for var0 three times--my text file has three lines. I changed the start of count from the above code from 1 to 0.

@echo off
SETLOCAL EnableDelayedExpansion
SET count=0
FOR /F "tokens=* delims= usebackq" %%x IN ("afile.txt") DO (
SET var!count!=%%x
echo !var%count%!
SET /a count=!count!+1
echo !count!
)
ENDLOCAL
Community
  • 1
  • 1
Rolo
  • 220
  • 2
  • 13
  • The problem is that `%count%` is not using delayed expansion and so isn't being incremented. Explain what you need to do because there are different solutions depending on what task is being performed. – foxidrive Sep 09 '14 at 14:29
  • I just want to show the results of the file. That's it. Then I will do something like start "" notepad.exe var1 var2 var3, but without me specifying the number after var. – Rolo Sep 09 '14 at 14:31
  • Add the command `set var` after the loop, which will display them. There's a different technique to concatenate a list of items into one string. – foxidrive Sep 09 '14 at 14:34
  • I am not understanding. What's the correct way to write the `%count%` part so that I can see the result of the text file being echoed each time? – Rolo Sep 09 '14 at 14:40

1 Answers1

2

While this code

echo !var%count%!

seems logic, as count is changed inside the loop, you can not access the value of the variable without delayed expansion. But

      echo !var!count!!
           ^...^     ^^
first variable        second "variable"

will not work, as the parser is not able to properly determine where each variable reference start/end

You can use any of the two following lines

for %%a in (!count!) do echo !var%%a!

call echo %%var!count!%%

How does it work?

The original idea is good, but there are limits in the syntax. We need delayed expansion over the var.. variable, but also over the count.

1.- In the first solution, we store the delayed expansion of the count variable inside the for replaceable parameter, that is used instead of the count variable in the echo command, keeping the delayed expansion only around the var variable.

!var!count!!   =>    %%a=!count!   =>   !var%%a!

2.- In the second solution, a call command is used. This call causes a double evaluation of the line, first on the line parse to execute the call, second while call execution. That is, the line

call echo %%var!count!%%

is first parsed, and the only variable referenced in it is !count! (a double percent sign is a single escaped percent sign), so the line is translated into (suppose count contains 5)

call echo %var5%

Now, the call is executed, line is parsed again to execute the echo and the value of the correct variable is retrieved.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • **Thank you!!!** This was driving me crazy. `!var!count!!` seemed to be the logical solution, but it's not. And everywhere I looked online, people would manually specify the echoing of the variables: i.e. var1, var2, and so on. I chose the 2nd one because it requires me changing the least amount of code. Plus, I am simply mimicking the above code and working with the areas I understand. **1. Can you please explain what's the purpose of the double percentages (%%)? 2. Also, for your 1st solution, where would I specify the filename?** – Rolo Sep 09 '14 at 15:43
  • @rolo, i've included more details in the answer. Hope it helps. For the seconds question, there is no file name. The `for` i have included retrieves the value from inside the variable, not any file – MC ND Sep 09 '14 at 15:59
  • Thanks again. I will have to re-read this a few times to be sure I fully understand it. Maybe you can help me with my other question, which is a continuation of this one: [Batch Files: How to concatenate each line in a text file?](http://stackoverflow.com/questions/25751630/batch-files-how-to-concatenate-each-line-in-a-text-file) – Rolo Sep 09 '14 at 18:48