Problems with a for loop

  • Thread starter Thread starter Antti H
  • Start date Start date
A

Antti H

Hi,
i am creating a nifty little batch script that will create a tar archive
from arguments read in file.txt, which is:
#file.txt
foo
faa
foo2

The readlines.bat is:

for %%q in (file.txt) do (
set FOO=%FOO% %%q
)


then I want to echo %FOO% and I expect it includes strings foo faa foo2
nicely on one row.
However, this is not the case. The output is as follows:
foo2 foo2 foo2

what the heck is up with that?

Please reply to my e-mail address also.

Thanks in advance!

PS: I need recommendations on a good book on windows batch scripting,
something that covers 2003 server also?
 
Antti said:
Hi,
i am creating a nifty little batch script that will create a tar archive
from arguments read in file.txt, which is:
#file.txt
foo
faa
foo2

The readlines.bat is:

for %%q in (file.txt) do (
set FOO=%FOO% %%q
)


then I want to echo %FOO% and I expect it includes strings foo faa foo2
nicely on one row.
However, this is not the case. The output is as follows:
foo2 foo2 foo2
Try this:

@echo off
setlocal
for /F %%q in (file.txt) do (
call set "FOO=%%FOO%%%%q "
)
echo %foo%

You need the call or use of delayedexpansion to expand the actual
value in an area enclosed in parenthes.
The trailing space is IMO more elegant.

HTH
 
Matthias said:
Try this:

@echo off
setlocal
for /F %%q in (file.txt) do (
call set "FOO=%%FOO%%%%q "
)
echo %foo%

You need the call or use of delayedexpansion to expand the actual
value in an area enclosed in parenthes.
The trailing space is IMO more elegant.

HTH

This worked. Thanks!

Antti H
 
Antti H said:
This worked. Thanks!

Interesting. That's two posts today on virtually the same problem, either of
which could have been given the answer given the other. Not only that, but,
in both cases, the OP came back to say the solution worked!

But I still remain convinced that, eventually, someone will ask the question
only to find that the answer does not work. Seems unlikely, but then,
quantum physics is only a theory...

/Al
 
Al said:
Interesting. That's two posts today on virtually the same problem, either of
which could have been given the answer given the other. Not only that, but,
in both cases, the OP came back to say the solution worked!

But I still remain convinced that, eventually, someone will ask the question
only to find that the answer does not work. Seems unlikely, but then,
quantum physics is only a theory...

/Al

I asked first. And in my case it did solve the problem. I am still
struggling with the script, so I don't know if it solved all my
problems.

I'll ask here if I run into overwhelming problems.

Antti H
 
Back
Top