Page 1 of 1

FOR /F Text Reading: Retain Empty Lines & Dynamic Skip?

Posted: 27 Jul 2020 16:43
by vin97
I have some problems with reading text files through FOR /F.

1 When a certain condition is met while looping through the file, I need to jump forward to a specific line. So I am looking for a way to change the value of "skip=" inside the loop.
2 I need to deactive the autoskip of empty lines, since it will mess up (desync) my algorithm.
3 Additionally, it would be great to get the current line number from FOR natively without having to count manually using a dedicated variable.

Re: FOR /F Text Reading: Retain Empty Lines & Dynamic Skip?

Posted: 28 Jul 2020 07:35
by Aacini
Well, the way to solve your problems in the way you want is to rewrite cmd.exe in the part that implements the FOR command! :shock: :lol:

However, if you still want a solution, then try this:
  1. Implement a read/no_read flag. Set it when you want to skip lines and reset it to continue reading lines.
  2. Use the findstr /N "^" trick to show all lines preceded by a line number combined with tokens=1* delims=: to get just the line contents, even if it is empty. This trick is documented in several sites.
  3. You may use the same previous trick in order to get the line number of the lines.
These solutions works very well and are simple to implement. At least, much simpler than try to "force" cmd.exe to do something it was not designed for!

Antonio

Re: FOR /F Text Reading: Retain Empty Lines & Dynamic Skip?

Posted: 28 Jul 2020 15:53
by vin97
1 Would still have to read the line, no? Ideally I would like to avoid even having to read a flag at the beginning of each line.
2 Doing this trick in a for loop with the type command is very slow for me, so I guess I will stick to filling empty lines beforehand.