1

I tried it like this:

for /F %%i in (%textfile%) do ( 
echo %%i )

Or this:

for /F "tokens=* delims=" %%i in (%textfile%) do ( 
echo %%i )

Or every other combination, with or without tokens or delims.

All of those combinations just echo every line,but for whatever reason excluding the ones starting with a semicolon. Using a semicolon as a delim defeats the purpous because i can't recognize wich line used to start with a semicolon and wich did not.

If there is any way of correctly reading the lines with a semicolon or recognizing the semicolon lines after they were removed from the delims?

MoritzP
  • 43
  • 4
  • 2
    Use this instead, ```@For /F UseBackQ^ Delims^=^ EOL^= %%G In ("%textfile%") Do @Echo(%%G```. This assumes that you have correctly defined the variable named `textfile`, i.e. ```@Set "textfile=P:\athTo\my file.txt"```. _Please note, that this will still exclude lines with no content_. – Compo Mar 22 '23 at 11:33
  • @Compo This works! Thank you! Mind explaining what all the "^" do? – MoritzP Mar 22 '23 at 11:41
  • 1
    They escape the character which is directly to their right! – Compo Mar 22 '23 at 11:44

1 Answers1

0

Exchange the default "End Of Line" character ; by something else:

for /f "tokens=* eol=^A" %i in (%textfile%) do @echo %i

(in a batch write %%i instead of %i)

The ^A is just the content of ASCII/ANSI code 1. You can obtain it by pressing the modifactor key alt and type on the numeric keypad 0001.

That's a character that rarely will appear in a text file. But your're free to choose a different one.


Re-Edit: As Compo pointed out in the comments, in fact for the solution there is no EOL character required. So no character will be blocked:

for /f tokens^=*^ eol^= %i in (%textfile%) do @echo %i

As jeb correctly mentioned, one cannot use the obligatory quotes here. So the special interpreted characters = and (space) have to be escaped by the ^.

At Compo's solution, also the field delims is set to 'empty' so the tokens option is not necessary in that case.

dodrg
  • 1,142
  • 2
  • 18
  • OK, I see the point, that one can define *no* character as EOL. The positive effects of `usebackq` is true, but not in the focus of the question. So I concentrated on that. – dodrg Mar 22 '23 at 12:19
  • 1
    Your sample with `for /f "tokens=* eol="` will use the quote as eol character. Only the strange Version from Compo disables the eol character – jeb Mar 22 '23 at 18:00
  • @jeb you're right. I didn't test the `"`as EOL.. A pitty that the cmd shell does not give an error because of the missing `"` that has been eaten by the `eol="`. — Nice construct... – dodrg Mar 22 '23 at 18:52