how to write this for loop?

T

thinktwice

set variable=oldvalue
for ... in (....) do (
set variable=newvalue
command1
command2
....
commandn)

but if command1-n depend on the value of variablen, it doesn't work.
cause when it runs to commandn, it still use the oldvalue. how to
write this for loop ?
 
F

foxidrive

On Mon, 5 May 2008 07:58:48 -0700 (PDT), thinktwice <[email protected]>
wrote:

@echo off
setlocal EnableDelayedExpansion
set variable=oldvalue
for ... in (....) do (
set variable=newvalue
command1 !variable!
command2 !variable!
....
commandn !variable!)
set variable=oldvalue
 
T

Tom Lavedas

set variable=oldvalue
for ... in (....) do (
set variable=newvalue
command1
command2
...
commandn)

but if command1-n depend on the value of variablen, it doesn't work.
cause when it runs to commandn, it still use the oldvalue. how to
write this for loop ?

SETLOCAL /? and SET /? Read about delayed expansion.

However, I don't think I understand what you mean by 'variablen' and
'commandn'. Your construct creates only one variable with changing
contents. However as shown the variable's content is the same for all
commands for each iteration through the loop.

You're just going to have to explain what you are trying to accomplish
in more detail. I don't understand what it is you are trying to do.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
 
P

Pegasus \(MVP\)

thinktwice said:
set variable=oldvalue
for ... in (....) do (
set variable=newvalue
command1
command2
...
commandn)

but if command1-n depend on the value of variablen, it doesn't work.
cause when it runs to commandn, it still use the oldvalue. how to
write this for loop ?

Have a look at the reply I gave you on about 30 April. It looked
likes so:
1. @echo off
2. setlocal enabledelayedexpansion
3. set Source=d:\temp\test.txt
4.
5. for /F "tokens=2,4 delims==" %%a in ('type "%Source%"') do (
6. set projectname=%%a & set projecttype=%%b
7. echo Project name=!projectname! Project type=!projecttype!
8. )
Now have a closer look at Line 2, and also at the exclamation
marks in Line 7. That's your answer!
 
T

thinktwice

thanks guys, DelayedExpansion is really what i need, really
appreciated your help. :D
 
T

thinktwice

hi, i met another problem. my command is like this:

setlocal enabledelayedexpansion
for ...in ('findstr ....') do (
set projecttype=%%i
set projectname=%%j
if (!projecttype!)=="xxx" echo mycommand > logdir\!projectname!.log
<----works ok!
findstr /i /s /c:Error(s) logdir\!projectname!.log <-----------doesn't
work
)
endlocal


why this happens?
 
T

thinktwice

findstr /i /s /c:"Error(s)" logdir\!projectname!.log

finally i find i should add double quote around the search string :)
 

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