Code: Select all
for /f "usebackq tokens=* delims=" %%a in (source.txt) do (
...
)
for /f will process the text within a file, a literal string, or the output of a command. To switch between these modes, the
in (...) after
%%a changes as follows:
Code: Select all
(C:\my_file.txt)
("My string.")
('echo:My command output.')
Note that you cannot use spaces in the path to the file, or certain special characters in that path. But... enable
usebackq and it changes as follows:
Code: Select all
("C:\my file.txt")
('My string.')
(`echo:My command output.`)
I only recommend using
usebackq if you're reading text from a file with a path that contains spaces, or special characters. Quotation marks toggle the command interpreter's processing of special characters irregardless of where you do it, so it's good to have them for string processing too. But there are some further tricks you can employ with quotation marks, without
usebackq:
And with
usebackq:
Code: Select all
('"My string."')
(`"echo:My command output."`)
You can process commands without escaping special characters by surrounding them with quotation marks, unlike the regular command line. You can process the quotation marks with a string, allowing you to control the toggling of special character interpretation one way or the other.
tokens=* means that you're only defining one token, and that token
%%a will contain all text within the line, sans any initial delimiters. However,
delims is set to nothing, so it will contain the full line of text regardless. This actually makes the
tokens=* option pointless.
As for
%%a, you can start the token defining at any alphabetical character in either lowercase or uppercase (it is case-sensitive). So, you could use %%z, but
for /f will not define characters beyond
%%z of course.
If you're wondering what tokens really means, if you set
tokens=1-3,5,7* and say,
%%D and have a line with many delimiters, you will define from
%%D to
%%I, with
%%I containing the token after 7 including the entire rest of the line (no longer delimiting). The
1-3 format is a range, so token 2 is also being defined.
%%D = token
1%%E = token
2%%F = token
3%%G = token
5%%H = token
7%%I = token
*