I have been trying for 2 days to get the superficially-simple script below working:
for /F "eol=*" %%A in (c:/users/SCTMP000/server.txt) do (echo %%A)
This itself is a reduction of my desired code, which I intended to scroll through the above text file, which is just a list of domains, in order to issue a PING
/ TRACERT
command against each domain and pipe the output to another text file. But even this simple one-liner won't process the file.
I've seen countless variations of the above cited on MSDN, StackOverflow, this site and many personal developer blogs, so feel that I'm in the right ball-park, but mine won't work ! Depending on how I render the file-name and its path (-ie quoteless, wrapped in single-quotes, wrapped in double-quotes), I see:
[quoteless] - nothing: no file open activity, and therefore no ECHO per line
[double-quoted] - the full path-name ECHOed, ie c:/users/SCTMP000/server.txt
[single-quoted] - the complete file actually opens in NotePad !!
So the path is correct, but neither the script run as a batch file, nor a command run interactively, seems to be able to actually open the text file and scroll through it. Note also that I have tried a number of the line-options: DELIMS, TOKENS, EOL etc, without success.
What am I doing wrong ? Thanks in advance.
Answer
I was able to get this to work with the explained results using a sample list file with domain names I placed in the list. I used the FOR /F "USEBACKQ TOKENS=*" %%A IN ("filelist")
just like that.
I try to use the USEBACKQ
and TOKENS=*
in FOR /F loops that read from a file list for the reasons I've listed below in the Script Logic Explained section so read that over and test it to confirm.
Working Batch Example
FOR /F "USEBACKQ TOKENS=*" %%A IN ("c:\users\SCTMP000\server.txt") DO (ECHO %%~A)
Script Logic Explained
The
USEBACKQ
option used in the FOR loop will ensure the file list can still be read if the file list name or it's path has any
spaces in it and you need to double quote the file list path
- E.g.
SET FileList=C:\Folder Name\File List.txt
- Without the
USEBACKQ
the FOR loop would error out in a case like this
The
TOKENS=*
option used in the FOR loop will ensure the the entire value is returned as it's read from the file list even if that
value has a space in it even though that should not be applicable to domains this is why you'd use it
E.g. File list has a value of
"test my file.txt"
so the value has a space on a line
- Without the
TOKENS=*
the FOR loop would only return the value portion of that line before the first space and not the value as expected (i.e."test"
)
Using these options even when not needed does not seem to cause any harm and should you ever introduce such a value or variable into the mix of the script, it'd already be able to handle such cases accordingly.
Further Resources
- FOR /F
- Troubleshooting Task Scheduler Tasks
FOR /?
tokens=x,y,m-n - specifies which tokens from each line are to
be passed to the for body for each iteration.
This will cause additional variable names to
be allocated. The m-n form is a range,
specifying the mth through the nth tokens. If
the last character in the tokens= string is an
asterisk, then an additional variable is
allocated and receives the remaining text on
the line after the last token parsed.
usebackq - specifies that the new semantics are in force,
where a back quoted string is executed as a
command and a single quoted string is a
literal string command and allows the use of
double quotes to quote file names in
file-set.
No comments:
Post a Comment