Jump to content

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


Recommended Posts

Guest Buddy Lott
Posted

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,

  • Replies 4
  • Created
  • Last Reply

Popular Days

Guest Pegasus \(MVP\)
Posted

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

 

 

"Buddy Lott" <BuddyLott@discussions.microsoft.com> wrote in message

news:94460634-69B5-4DAF-B70A-D0D89DA358CF@microsoft.com...

> 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.

Guest Pegasus \(MVP\)
Posted

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

 

 

"Buddy Lott" <BuddyLott@discussions.microsoft.com> wrote in message

news:94460634-69B5-4DAF-B70A-D0D89DA358CF@microsoft.com...

> 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.

Guest Buddy Lott
Posted

Re: Why is an enviroment variable not updated correctly in a for l

 

Re: Why is an enviroment variable not updated correctly in a for l

 

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

)

 

"Pegasus (MVP)" wrote:

>

> "Buddy Lott" <BuddyLott@discussions.microsoft.com> wrote in message

> news:94460634-69B5-4DAF-B70A-D0D89DA358CF@microsoft.com...

> > 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.

>

>

>

Guest Pegasus \(MVP\)
Posted

Re: Why is an enviroment variable not updated correctly in a for l

 

Re: Why is an enviroment variable not updated correctly in a for l

 

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!

 

 

"Buddy Lott" <BuddyLott@discussions.microsoft.com> wrote in message

news:9EC548CF-C42A-4E89-BAFF-BA534BE56F99@microsoft.com...

> 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

> )

>

> "Pegasus (MVP)" wrote:

>

>>

>> "Buddy Lott" <BuddyLott@discussions.microsoft.com> wrote in message

>> news:94460634-69B5-4DAF-B70A-D0D89DA358CF@microsoft.com...

>> > 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.

>>

>>

>>


×
×
  • Create New...