Reading from multiple .txt files in Batch

September 6, 2010 at 08:32:16
Specs: Windows XP
I'm making a program in Batch that opens up multiple .txt files and gets data from all of them, but it seems to only load one .txt file.
Here's and example of what I mean:
text1.txt
blue

text2.txt
green

text3.txt
yellow

code
ECHO OFF
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (ms.txt) do (
set /a N+=1
set v!N!=%%a
set dir=%cd%
)
set color1=!v1!

for /f "tokens=* delims= " %%a in (ms.txt) do (
set /a N+=1
set v!N!=%%a
set dir=%cd%
)
set color2=!v1!

setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (ms.txt) do (
set /a N+=1
set v!N!=%%a
set dir=%cd%
)
set color3=!v1!

ECHO %color1%
ECHO %color2%
ECHO %color3%
pause

The output would be:
blue
blue
blue
Press any key to continue...

Cheers to anyone who actually read all of this. :)


See More: Reading from multiple .txt files in Batch

Report •


#1
September 6, 2010 at 09:50:23
You never reset !N! to zero and there is no need to read the file three times, you have everything you need in vn.

Even if you did reset !N! to zero you are always asking for "V1" which will always be blue, because it's the first line.

Edit

You also mention three files and read only "ms.txt"

set dir=%cd%

Seems to be doing nothing since you don't even change directories.


Report •

#2
September 6, 2010 at 10:01:25
I don't get what it's trying to do.


=====================================
Helping others achieve escape felicity

M2


Report •

#3
September 7, 2010 at 13:58:09
I put in the set N=0s I forgot, it still, however, only outputs blue.
In my example it reads from ms.txt, which is a mistake. It's supposed to read from text1.txt, text2.txt, so on, so forth. Don't have any idea what could be wrong, so I'll just try rewriting from scratch.

Report •
Related Solutions


Ask Question