2

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.

  • there's no for /f in DOS. cmd and DOS are completely different things – phuclv Nov 13 '17 at 17:06
  • Thank you for your response; yes, I'm afraid a legacy of my age - I've been using CMD-like tools since 1989, and sometimes forget that newer tools get rolled out ! This is indeed CMD.EXE. Am currently trying the command on 2 other Win 10 machines. – robgoch Nov 13 '17 at 18:08
  • Your line works just fine for me on Windows 10. I think your problem lies elsewhere. Perhaps a permissions issue, where Explorer or Notepad can access the file but your CMD instance can't? I suggest you try to isolate the problem: move the file elsewhere, create a new file with the same name in its place, try a different file in a different place altogether - see what works and what doesn't and use that to infer where the problem lies (e.g. the file, the path, the containing folder, ...). – Tomer Godinger Nov 13 '17 at 20:40
  • Now that I think of it, you should probably check the contents of your file with a hex viewer/editor and make sure it's really just text and there's nothing suspicious at the beginning. If your file starts with a zero value, for example (0x00), you would get exactly the behavior you described: the FOR loop would print nothing but Notepad would be able to open the file just fine (it might show a space or something at the beginning, but it'll open it all the same). – Tomer Godinger Nov 13 '17 at 20:47
  • Thanks, Tomer; rather predictably, it's working fine on one of my Win 10 laptops at home, so I'm guessing that my place of work has some strange permissions with respect to CMD: the senior network folk are very PowerShell-oriented (-perhaps I should be the same ?!?), and probably haven't focussed recently on the pitfalls of testing CMD batch files. I'll get one of our administrators to check my user. – robgoch Nov 13 '17 at 21:02
1

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.
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.