Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
853 views
in Technique[技术] by (71.8m points)

batch file - How to disable both `eol` and `delims` options of `for /F`?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...