HOW TO: FOR /F Disabling EOF or using a quote as delim
Posted: 27 Oct 2011 16:22
Hi,
I repeat here some description from other posts, as it's a bit hard to find them anymore, and the corresponding threads are quite long.
I will try to explain some advanced FOR /F tricks/usages
1. Disabling of the EOL character
If the first character of a line is the EOL-character, the line will be ignored.
Sometimes you didn't want any EOL-character, but it's not obvious how to disable it.
As this only sets the EOL-Standard semicolon to a quote character
In my opinion the EOL option reads always the next character and doesn't regards any special characters.
But dbenham found a good trick how to disable it, if you use one ore more delim-characters,
if the EOL character is also a delim character the EOL is disabled at all.
But how to disable it, if the delims list is empty?
You could use a line feed character, as this is always the line termination character, the effect is like the EOL is disabled.
But it's not so obvious how to set the EOL to the line feed character, but it's possible.
This isn't a typo, the empty line is necessary, also the carets, as the key is the multiline caret at the end of line1,
but this can only work without quoting, therefore the first quote is escaped too.
But as the quote is escaped all batch line delimiters (not the FOR/F delims-option) are comma, semicolon, tab,space and the equal sign have to be escaped now with a caret.
And then there is the old question: "Is it possible to use the quote itself as a delim-character?"
Yes, but it's much more worse than the EOL-line feed trick.
FOR /F tokens^=1^,2^,3^ delims^=^" %%A in ("one"two"three") do echo %%A--%%B--%%C
This trick is found by pieh-ejdsch at adminstrator.de: FOR loop double quotes as Delimiter
jeb
I repeat here some description from other posts, as it's a bit hard to find them anymore, and the corresponding threads are quite long.
I will try to explain some advanced FOR /F tricks/usages
1. Disabling of the EOL character
If the first character of a line is the EOL-character, the line will be ignored.
Sometimes you didn't want any EOL-character, but it's not obvious how to disable it.
As this only sets the EOL-Standard semicolon to a quote character
Code: Select all
FOR /F "EOL=" %%A in ("test") do echo %%A
In my opinion the EOL option reads always the next character and doesn't regards any special characters.
But dbenham found a good trick how to disable it, if you use one ore more delim-characters,
if the EOL character is also a delim character the EOL is disabled at all.
Code: Select all
FOR /F "EOL=x delims=xyz"
But how to disable it, if the delims list is empty?
You could use a line feed character, as this is always the line termination character, the effect is like the EOL is disabled.
But it's not so obvious how to set the EOL to the line feed character, but it's possible.
Code: Select all
FOR /F ^"tokens^=1^,2^ EOL^=^
^" %%A in ...
This isn't a typo, the empty line is necessary, also the carets, as the key is the multiline caret at the end of line1,
but this can only work without quoting, therefore the first quote is escaped too.
But as the quote is escaped all batch line delimiters (not the FOR/F delims-option) are comma, semicolon, tab,space and the equal sign have to be escaped now with a caret.
And then there is the old question: "Is it possible to use the quote itself as a delim-character?"
Yes, but it's much more worse than the EOL-line feed trick.
FOR /F tokens^=1^,2^,3^ delims^=^" %%A in ("one"two"three") do echo %%A--%%B--%%C
This trick is found by pieh-ejdsch at adminstrator.de: FOR loop double quotes as Delimiter
jeb