Question on Simple nested FOR

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mimismo
Posts: 11
Joined: 11 Jun 2016 07:59

Question on Simple nested FOR

#1 Post by mimismo » 16 Jun 2016 21:30

Hi

I have a text file named "All Files.txt" containing a list of another csv files.
I want to get a variable that contains each row value in the nested FOR of the csv files listed in "All Files.txt"
Let me show my code and the output

Code: Select all

@echo on
setlocal EnableDelayedExpansion

for /F "tokens=*" %%B in ("All Files.txt") do (
   set Tem=
   for /F "tokens=*" %%a in ("%%B") do (
      set Tem="%%a"
      echo Var=%%a
      echo inside=!Tem!
   )
   echo outside=!Tem!
)


The content of each file is:

Code: Select all


D:\Test\Test2>type "All Files.txt"
File One.csv
File Two.csv
File Three.csv

D:\Test\Test2>type "File One.csv"
Test One,10
Test Five,8
Test Seven,3
Test Ten,2
Test Eleven,20

D:\Test\Test2>type "File Two.csv"
Test Four,5
Test Six,1
Test Nine,21
Test Twelve,40
Test Twenty,10
Test Twenty One,1
Test Twenty four,4


D:\Test\Test2>type "File Three.csv"
Test One,1
Test Two,3
Test Four,7

D:\Test\Test2>


The output I got from my code is show here:

Code: Select all

D:\Test\Test2>Test3.bat
D:\Test\Test2>setlocal EnableDelayedExpansion

D:\Test\Test2>for /F "tokens=*" %B in ("All Files.txt") do (
set Tem=
 for /F "tokens=*" %a in ("%B") do (
set Tem="%a"
 echo Var=%a
 echo inside=!Tem!
)
 echo outside=!Tem!
)

D:\Test\Test2>(
set Tem=
 for /F "tokens=*" %a in ("All Files.txt") do (
set Tem="%a"
 echo Var=%a
 echo inside=!Tem!
)
 echo outside=!Tem!
)

D:\Test\Test2>(
set Tem="All Files.txt"
 echo Var=All Files.txt
 echo inside=!Tem!
)
Var=All Files.txt
inside="All Files.txt"
outside="All Files.txt"

D:\Test\Test2>


What I'm expecting is getting for the "echo inside" the content of the csv files but instead I'm getting the content of the txt file.
How can I get the content of each row in the nested FOR of the csv files in a variable Tem for further treatment?

Did I miss something?
Please share your thoughts
Thank you in advance

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Question on Simple nested FOR

#2 Post by Aacini » 16 Jun 2016 22:17

When the set in FOR /F command is a string enclosed in quotes, then the FOR /F process the string itself. For example:

Code: Select all

for /F "tokens=*" %%B in ("Process this string") do echo String: "%%B"

... show String: "Process this string".

If you want to process a file name enclosed in quotes, then you must include the "usebackq" switch in the FOR /F options:

Code: Select all

for /F "usebackq tokens=*" %%B in ("Process this FILE.txt") do echo Lines: "%%B"

For further details, type FOR /? and carefully read the description of FOR /F and the examples on "usebackq".

Antonio

mimismo
Posts: 11
Joined: 11 Jun 2016 07:59

Re: Question on Simple nested FOR

#3 Post by mimismo » 16 Jun 2016 22:47

Thank you Antonio, now the code works well

A quick question, does it matter if I use "usebackq" switch in the outside FOR or it should be in the inside FOR.
I tested in both ways and it works and the only difference was that in the outside FOR, the output repeats the source code in each iteration and the switch "usebackq" in the inside FOR, it only give me the result of the variables.

Thank you again.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Question on Simple nested FOR

#4 Post by sambul35 » 17 Jun 2016 01:30

Or, instead of using "usebackq", just rename the file to All_Files.txt and remove the quotes around its name.

This site is an excellent first step Help source about batch commands, if you read it attentively. The forum is the d) fourth step source, when you a) used commands properly as advised by Help, b) searched the web for proper code samples for your tasks, c) tested your improved code and still didn't get anywhere. At that point explain, what result did you expect by running your code sample versus what you get.

Most of your questions posted so far show lack of attention code errors and no use of command & operator Help. :wink:

Post Reply