Solved Reading a text document

October 31, 2016 at 12:34:51
Specs: Windows 7
I'm using the following in an attempt to set a variable to X.
I want it to do this and then stop.
I will call this again later or run the batch again and the hope was that the %LINE% variable would call the next in line for the hostlist.txt.
As it is, it doesn't work the first time, and just sets %Line% to 1. (understandable since %Line% is unspecified.
It works the second time, but it goes through the whole entire list resetting x over and over and ends at setting the variable to the very last one on the list. Running the 3rd time just sets %Line% to 3.

I deduce that tokens doesn't mean what I thought they did. It seems to mean column 1 which is why it works when %line% is set to 1, but not after that since there is nothing in column 2.
What type of parameter or whatever it is do I need to read line by line?
I'd prefer if the %Line% could still be used so it would remember what line it left off at.

for /f "eol=, tokens=%Line% delims=" %%x in (hostlist.txt) do Set X=%%x
set /A Line=%Line% + 1
echo %Line%

The hostlist.txt looks like this:

(so on for a long long while)

message edited by scionave

See More: Reading a text document

Report •

October 31, 2016 at 16:35:50
✔ Best Answer
Something like this?

setlocal enabledelayedexpansion
for /f "skip=%Line% eol=, tokens=*" %%x in (hostlist.txt) do (
        set X=%%x
        set /a line+=1
        goto :break

If you want to use variables that change in a for loop you must enable delayed variable expansions.

Using %Line% at tokens= would in the end create something like "tokens=317". Your strings do not contain 317 tokens, and you haven't specified any delimiter either, so there is no point in using tokens at all. What you're looking for is probably tokens=*, which means the entire line.

To make it do it once, you can use a break, hence the "goto :break", then the ":break" label after the loop. To save which line it's on, and make it continue from where it left off, you can use "skip=" in the FOR command. For every iteration, %Line% increments by one before breaking, meaning it will resume at the line where it left off.

You should only need to use "setlocal enabledelayedexpansion" once, so I recommend placing the command as early as you can in the script.

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ

Report •

November 4, 2016 at 06:32:06
When I use exactly what you gave me, I get eol=, tokens=*" was unexpected at this time. When I type "Set Line" to see what Line was set to, it tells me that Environment variable Line not defined.

*EDIT* I think I figured it out. It can't because Skip at first is 0 and it doesn't like that. I have to first define Line to at least 1 somewhere first. Thanks!

Okay, now it's taking the , as part of the host name even thought it's specified by eol. Not sure why. I just took the , with find and replace out of the hostname.txt. That works too. Thanks again for all the help.

message edited by scionave

Report •
Related Solutions

Ask Question