Using FOR command to process lines in a single file

I feel stupid for asking this but this is something I've not done before.  I've used FOR a lot to parse a command (i.e. dir *., date /t, etc.) to then process that info in some way - reset permissions on a set of folders, set and environment variable to the date and time, etc.
This time I want to take a single file (SvrFile.txt) that contains a list of servers, such as this example:


Shouldn't the FOR command be able to read each line and echo the server names to the screen?  At best I can only get the command to list the text file itself:

          for /f "eol=~" %G IN ("c:\kix32\svrfile.txt") do echo %G

This produces:

          C:\KIX32>echo c:\kix32\svrfile.txt

I can easily script this task - and I have - and I know it's really "old school" but now the question is sticking in my head and I haven't found a pertinent example to follow.

Any help would be appreciated.

Kevin Murphy
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kmurphyAuthor Commented:
I actually realized I should be processing the find command on this file and that does give me the contents when I parse "find /V "" svrfile.txt"

so here's how I get the list of the lines in the file:
for /F "eol=~" %G IN ('find /V "" c:\kix32\svrfile.txt') do echo %G
NVITEnd-user supportCommented:
Other ways...

for /f %a in ('type "c:\kix32\svrfile.txt"') do echo %a
for /f %a in (c:\kix32\svrfile.txt) do echo %a

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
If you want to enclose the file to read in double quotes, then you need to add the "usebackq" to the options.  So something like this:

for /f "usebackq eol=~" %G IN ("c:\kix32\svrfile.txt") do echo %G

There's no need to use FIND unless you are selecting only certain lines?


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.