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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vin97
Posts: 35
Joined: 17 Apr 2020 08:30

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

#1 Post by vin97 » 27 Jul 2020 16:43

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.

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

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

#2 Post by Aacini » 28 Jul 2020 07:35

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

vin97
Posts: 35
Joined: 17 Apr 2020 08:30

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

#3 Post by vin97 » 28 Jul 2020 15:53

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.

Post Reply