1.

Solve : How to run more RELATED commands with the same variable read from a text file??

Answer»

Hello!

I have a dilemma of that I need to run commands that depend on the result of the previous one. How do you do that with a FOR loop?

If I'd rephrase this question, it would say: How can you run more than one command with a word picked from a text file?

For example I have a text file "computers.txt" that has the LIST of all active computers like this:

cp001
cp002
cp005
cp022
etc.

Then I want the batch file to pick the first computer name, then do these or any multiple commands with the same computer name:

a) if not exist "\\%computer%\c$\program files\eudora\nicknames\sample.txt" copy whateverfilename.txt "\\%computer%\c$\program files\eudora\nicknames\"

b) echo %computer%>>resultlog.txt

...then go and pick the next computer name from the file computers.txt and run the same commands again until it finished reading the last name from the text file.

Geza

for /f "delims==" %%A in (computers.txt) do (
set computer=%%A
if not exist "\\%computer%\c$\program files\eudora\nicknames\sample.txt" (
copy whateverfilename.txt "\\%computer%\c$\program files\eudora\nicknames\"
REM put this here to log only when a file needed copying
echo %computer%>>resultlog.txt
)
REM or here to log every computer on the list regardless
echo %computer%>>resultlog.txt
)


did you want the echo to resultlog.txt whether or not sample.txt is copied?





Contrex,

You just made my day!!

Who would have THOUGHT of, that the solution is using parenthesis??
I didn't.

(I expected some kind of multiple For/Next loops, that I learned in Algol, more than 30 years ago.)

I've been using batch files in my work (network administrator) in the past 15 years but I could never figure out the solution to this problem, (For /? was no help in this matter) so when I needed to run a batch file for the same result, I used excel and notepad to create a batch file with as many lines as many I needed and used "find/replace" all - heavily and used IF and GOTO like crazy.

No, I don't need conditional %errorlevel% reporting, but Thank You for offering it to me, the batch file example mentioned in my question was there just for the sake of an example.

I will have an another question though, but I won't clutter this thread with it, because it is a different subject, I will start a new one a bit later.

Thanks a bunch again for the solution!!!

Geza
I found this command on a page of a similar question:

<
REM For loop example to pull each entire line from another file
REM Note that each line is executed independantly from all others

for /f "tokens=*" %%a in (file.txt) do CMD /C %%a
>

where the only difference is in between the quotes:

"tokens=*" and "delims=="

What is the difference? Or, what is the meaning of the 2nd "=" after "delims="?

Can you recommend me a good book or maybe a website where I can learn more about this stuff in this dept?

Thanks,
Geza

------------------------------------------------------------------------
delims=xxx - specifies a delimiter set. This replaces the
default delimiter set of space and tab.
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.
------------------------------------------------------------------------Hi guys I'm a girl from 75 I use to play Janitor Joe a lot but now I tried to play it after I founded free on the net however is running way to fast I can't get it to work. what do I need to do any IDEA?75? LE soixante-quinze (Paris)? Alors, chérie, il faut que tu commences ton propre fil...Quote from: Geza on July 28, 2007, 12:15:23 PM

"tokens=* delims=="

What is the difference? Or, what is the meaning of the 2nd "=" after "delims="?

As far as I can see, "tokens=*" means "no token separators", ie parse the whole line,

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. (IE if the asterisk is the first and last character then there is no last token, and the whole text of the line is passed to the first variable)

... and "delims==" is equivalent to "delims=", ie "token delimiters are the start and end of the line",

so this options block:

"tokens=* delims=="

is a kind of dummy placeholder meaning "process the whole line and don't split it".

examples...

Quote
@echo off
echo data set="eggs bacon tomatoes"
echo.
echo "delims="
for /f "delims=" %%A in ("eggs bacon tomatoes") do (
echo variable A=%%A
)
echo.
echo "delims=="
for /f "delims==" %%A in ("eggs bacon tomatoes") do (
echo variable A=%%A
)
echo.
echo "delims=*"
for /f "delims=*" %%A in ("eggs bacon tomatoes") do (
echo variable A=%%A
)
echo.
echo "delims= "
for /f "delims= " %%A in ("eggs bacon tomatoes") do (
echo variable A=%%A
)
echo.
echo "tokens=1,2,3 delims= "
for /f "tokens=1,2,3 delims= " %%A in ("eggs bacon tomatoes") do (
echo variable A=%%A
echo variable B=%%B
echo variable C=%%C
)
echo.
echo data set="eggs,bacon,tomatoes"
echo.
echo "tokens=1,2,3 delims=,"
for /f "tokens=1,2,3 delims=," %%A in ("eggs,bacon,tomatoes") do (
echo variable A=%%A
echo variable B=%%B
echo variable C=%%C
)
echo.

echo data set="eggs,bacon,tomatoes,sausages,fried Velveeta,corn pone,kippers"
echo.
echo "tokens=1,2,3,4* delims=,"
for /f "tokens=1,2,3,4* delims=," %%A in ("eggs,bacon,tomatoes,sausages,fried Velveeta,corn pone,kippers") do (
echo variable A=%%A
echo variable B=%%B
echo variable C=%%C
echo variable D=%%D

echo.
echo extra variable called into being by asterisk at end of "tokens=" string
echo variable E=%%E
)


output...

Quote
data set="eggs bacon tomatoes"

"delims="
variable A=eggs bacon tomatoes

"delims=="
variable A=eggs bacon tomatoes

"delims=*"
variable A=eggs bacon tomatoes

"delims= "
variable A=eggs

"tokens=1,2,3 delims= "
variable A=eggs
variable B=bacon
variable C=tomatoes

data set="eggs,bacon,tomatoes"

"tokens=1,2,3 delims=,"
variable A=eggs
variable B=bacon
variable C=tomatoes

data set="eggs,bacon,tomatoes,sausages,fried Velveeta,corn pone,kippers"

"tokens=1,2,3,4* delims=,"
variable A=eggs
variable B=bacon
variable C=tomatoes
variable D=sausages

extra variable called into being by asterisk at end of "tokens=" string
variable E=fried Velveeta,corn pone,kippers

Quote
Can you recommend me a good book or maybe a website where I can learn more about this stuff in this dept?

You could type "FOR loop tutorial" into Google. This one is pretty good.

http://www.student.oulu.fi/~vtatila/batch_tutorial.html#forContrex,

Last night I noticed, that the "new reply notification" messages from this website were caught by my spam filter, that's why I didn't notice your reply until now.

Thanks a lot for explaining to me basically the difference between the terms "tokens" and "delims" and how can you use any of them for indicating "take the whole line in a text file".

Also, thanks for the link to Veli-Pekka's tutorial page on using "For", it is a GREAT page for learning DOS scripting!

Geza


Discussion

No Comment Found