Page 1 of 1

Nested for loops?

Posted: 31 May 2011 11:08
by mid_life_crisis
Not sure if this should be nested fors or a for with a test. I can do one or the other but can't figure out how to do both.
I need to read every line from one text file and write them to another file. That I can manage.
I also need to read the fifth value (tab delimited) and test its value and do something depending on the test result (this I can do).
The problem is that I cannot figure out how to do both. Read the entire line, read the fifth value, perform the test and write the entire line to a different file if the test passes.

For example:

for /F "tokens=5 delims= " %%H in (infile.txt) do (
setlocal enableDelayedExpansion
if NOT %%H==00:00 echo %%H >>outfile.txt
endlocal
)

Reads each line, saves the fifth value and if it is not 00:00 writes the value to a second file. I can't figure out how to make it write the entire line.

Re: Nested for loops?

Posted: 31 May 2011 11:24
by orange_batch
So, you just want it to see if the 5th value matches something, you want it to write the entire line to a file if it does? Nested fors is correct.

Code: Select all

for /f "tokens=*" %%a in (infile.txt) do for /f "tokens=5" %%b in ("%%a") do if "%%b"=="00:00" echo:%%a>>outfile.txt

You could do it in a single for, but you would have to work with a bunch more tokens and recreate the delimiter between them, which can be error-prone. Just for example's sake:

Code: Select all

for /f "tokens=1-5*" %%a in (infile.txt) do if "%%e"=="00:00" echo:%%a %%b %%c %%d %%e %%f>>outfile.txt

...where the spaces between %%a %%b... etc match the appropriate delimiter (and hoping there are values past token 5, otherwise remove " %%f" or you'll have a trailing space/delimiter on each line). See what I mean about error-prone...

Re: Nested for loops?

Posted: 31 May 2011 11:43
by dbenham
See what I mean about error-prone...
And there might have been multiple spaces between tokens in the original line, another error!

Nested loops is the way to go. But "tokens=*" results in leading spaces being stripped. And also beware of eol. You don't want lines starting with ; to be stripped! Improvement can be made using FOR /F "eol= tokens=*", but leading spaces will still be stripped. For ultimate solution see tail end of this post.

Dave Benham

Re: Nested for loops?

Posted: 31 May 2011 11:56
by orange_batch
Thanks for adding the tidbits Dave, I just want to say I'm not ignorant of it stripping leading delimiters, and the other problems, but it's usually irrelevant. Of course, multiple delimiters side-by-side won't reproduce properly without advanced stuff. 8)

The better question is why one would have a file in such a mess, and in that case why one would be using Command Prompt to process text, which it is bad at.

Re: Nested for loops?

Posted: 31 May 2011 12:41
by mid_life_crisis
for /f "tokens=*" %%a in (infile.txt) do (
for /f "tokens=5 delims= " %%b in ("%%a") do (
if NOT "%%b"=="00:00" echo %%a>>outfile.txt
)
)

This works and I thank you. You will notice that, for reasons unknown, the colon after echo had to be removed.
The file in question is the output from a program over which I have no control. It provides records even when the value is zero. The program into which the data is being imported is less than happy when it gets zero value records imported, so those records need to be dropped.
Again, I thank you all for the help.