I can input the variable to the batch file okay using set /p, but no matter what I do I get an error that the file cannot be found and the error always shows the variable name that I used to collect the filename.
Can someone explain the syntax of this?
for /f "usebackq tokens=* delims=" %%a in (source.txt) do (
Thanks
Keyboard input of source file in "FOR /f" loop
Moderator: DosItHelp
-
- Posts: 22
- Joined: 26 May 2011 07:56
-
- Posts: 126
- Joined: 10 Jun 2011 10:30
Re: Keyboard input of source file in "FOR /f" loop
Code: Select all
@echo off
Set file=source.txt
for /f "usebackq tokens=* delims=" %%a in (%file%) do Echo %%a
You can get the value for %file% anyway you choose like Set /P for instance...
-
- Posts: 126
- Joined: 10 Jun 2011 10:30
Re: Keyboard input of source file in "FOR /f" loop
I tried to get the SET /P Variable= command into the actual FOR /F loop and get it to expand to a single filename but failed... There may be a way to do it, but the example I posted previously works and I'm still trying to learn my way around variable expansion and how CMD parses.
-
- Posts: 22
- Joined: 26 May 2011 07:56
Re: Keyboard input of source file in "FOR /f" loop
Acy Forsythe wrote:Code: Select all
@echo off
Set file=source.txt
for /f "usebackq tokens=* delims=" %%a in (%file%) do Echo %%a
You can get the value for %file% anyway you choose like Set /P for instance...
I swear I tried that and it didn't work. This time it does.
Thank you.
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Keyboard input of source file in "FOR /f" loop
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:
Code: Select all
('"echo:My command output."')
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 *