Why is an enviroment variable not updated correctly in a for loop.

G

Guest

If I run the following script in a directory with two files named test1.txt &
test2.txt, FILELIST should have the value of "Test2.txt;Test1.txt;NOFILES".
Instead it has the value of "Test2.Txt;NOFILES". Can any one tell me why? Or
how to get it to work correctly?

@SET FILELIST=NOFILES
@for %%f in (*.txt) do @(
@echo %%f
@set FILELIST= %%f;%FILELIST%
@echo %FILELIST%
)

Thanks,
 
P

Pegasus \(MVP\)

Buddy Lott said:
If I run the following script in a directory with two files named
test1.txt &
test2.txt, FILELIST should have the value of
"Test2.txt;Test1.txt;NOFILES".
Instead it has the value of "Test2.Txt;NOFILES". Can any one tell me why?
Or
how to get it to work correctly?

@SET FILELIST=NOFILES
@for %%f in (*.txt) do @(
@echo %%f
@set FILELIST= %%f;%FILELIST%
@echo %FILELIST%
)

Thanks,

Try this instead:

@echo off
SetLocal EnableDelayedExpansion
SET FILELIST=NOFILES
for %%f in (*.txt) do (
echo %%f
set FILELIST= %%f;!FILELIST!
echo !FILELIST!
)

Run set /? from a Command Prompt to find out more
about delayed expansion.
 
P

Pegasus \(MVP\)

Buddy Lott said:
If I run the following script in a directory with two files named
test1.txt &
test2.txt, FILELIST should have the value of
"Test2.txt;Test1.txt;NOFILES".
Instead it has the value of "Test2.Txt;NOFILES". Can any one tell me why?
Or
how to get it to work correctly?

@SET FILELIST=NOFILES
@for %%f in (*.txt) do @(
@echo %%f
@set FILELIST= %%f;%FILELIST%
@echo %FILELIST%
)

Thanks,

Sorry, I meant

for /?

at the Command Prompt.
 
G

Guest

The case I posted works using the SetLocal EnableDelayedExpansion.... but it
doesn't seem to work for this case. Why?

SetLocal EnableDelayedExpansion
@FOR /R %%f IN (*.cpp *.c) DO (

if exist "%%~nf.lnt". SET FILE_SPECIFIC_CONFIG="%%~pf%%~nf.lnt".
if exist "%~p1\pclint.lnt". SET DIR_SPECIFIC="%~p1\pclint.lnt".
lint-nt "-os(%%~pf%%~nf.lint)" %LINT_INCLUDE% !DIR_SPECIFIC!
!FILE_SPECIFIC_CONFIG! u:\Projects\BAC\B_BC\sw\BC.lnt -u %%f
)
 
P

Pegasus \(MVP\)

I don't think that the syntax FOR /R %%f IN (*.cpp *.c) DO
is a valid use of wild cards. Since your batch files are quite
advanced, I recommend that you post your questions here:

alt.msdos.batch.nt

They love to dig their teeth into this sort of thing!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top