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.
Nested for loops?
Moderator: DosItHelp
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Nested for loops?
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.
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:
...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...
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?
And there might have been multiple spaces between tokens in the original line, another error!See what I mean about error-prone...
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
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Nested for loops?
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.
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.
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.
-
- Posts: 22
- Joined: 26 May 2011 07:56
Re: Nested for loops?
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.
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.