3

The for command with the /F switch is used to parse lines of text strings (literal strings, read from text files, or retrieved from command line output) and to parse each to one or more tokens.

How can I get a line of text as it is, without any characters replaced nor any lines ignored?

I know that for /F "delims=" %L in (*) do echo."%L" returns each parsed (non-empty) line unedited. However, since the eol option defaults to ;, every line starting with that character will be ignored.
If I use for /F "tokens=* eol=" %L in (*) do echo."%L", I disable the eol option [Edit: This claim is not true, "eol=" does not disable the eol option, but it defines " as the eol character!], but the delims option defaults to space and tab, so any leading spaces and/or tabs become removed.
(Use %%L within batch files. The * stands for any valid source of text string here.)

So my question in other words: is there a way to specify no delims and no eol characters?

I tried to specify two option strings ("eol=" "delims=") but such results in a syntax error. So does option string "eol=delims=".

Note: This problem does not persist when tokenizing, that is, when the delims option is set, because that seems to be applied with a higher priority than eol (strangely but luckily), so you can "hide" eol behind delims by specifying a delims character also as eol.

aschipfl
  • 33,626
  • 12
  • 54
  • 99

1 Answers1

4

1) you will be never able to force bat to not ignore empty / delimiter-only lines.This can be (sort-of) worked around by piping to findstr /R /N "^" command and use options like "tokens=1* delims=:" and get only the second token

2) To deactivate eol and delim at the same time you can use this syntax:

For /f tokens^=*^ delims^=^ eol^= %%a in (file.txt) do echo.%%a

though the empty lines still will be ignored.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 1
    1) yes, I'm aware of the fact that empty lines are skipped; the work-around usinf `findstr` is a good idea although it fails when a line starts with `:`; 2) thanks a lot for this syntax, I've never seen this before; does this define the space to be the delimiter?? translated into the standard quoted version it would read `"tokens=* delims= eol="` when I got it correct, right? – aschipfl Aug 24 '15 at 23:38
  • 1
    ad 2) I just found out that `"[...] eol="` defines the `"` as end-of-line char. (how stupid is that??); thus, thanks again for the hint to the non-quoted version!! I also found out that `delims` must be the last option only in case you want define a space as the delimiter; so in this case, when you want that together with no `eol`, the only chance is to provide `eol^=^^delims^=^`; is that correct, or is there a simpler (more legible) variant? – aschipfl Aug 25 '15 at 01:04
  • 3
    The `tokens^=*^ ` does no harm, but neither does it serve any purpose. All you need is `for /f delims^= eol^= %%a in ...` – dbenham Aug 25 '15 at 03:48
  • 1
    @aschipfl 1) With `findstr /n` you can build a [bullet proof loop](http://stackoverflow.com/a/5274061/463115), as each line begins with a number you don't get any problems 2) You don't need the linefeed version, the `delims^=^ ...` is enough. – jeb Aug 25 '15 at 06:24
  • 1
    Thanks, @jeb! 1) I meant `findstr /N` to fail with `delims=:` for leading `:`s, but I didn't think of `var:*:=:` expansion; 2) the line-feed version is needed for _no_ `eol` but `delims=`, right? – aschipfl Aug 25 '15 at 07:11
  • @jeb, 2) finally I found a solution for specifying _no_ `eol` but a `delims=` avoiding the version: `"eol= delims= "` (it seems the `eol` can be "hidden" behind `delims` if the latter is given, so `delims` has got a higher priority than `eol`); – aschipfl Sep 02 '15 at 23:43
  • @dbenham, do you know if there is any difference in performance between `"tokens=* delims="` and `"delims="`? – aschipfl Sep 02 '15 at 23:45
  • how to use variable tokens in the `for .. eol^= ..` ? for example: `set "v=2" for %%g in (1 2 3) do ( set "v=%%g" for /f tokens^=!v!*^ delims^=^ %%h in ("11 22 33 44") do echo %%h )` . `![_]!`, `%%[_]` , `%~[number]` and `%[number]`, with or without the ^ , doesn't work . – ilias Jan 20 '20 at 18:39