Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to write this for loop?

3 views
Skip to first unread message

thinktwice

unread,
May 5, 2008, 10:58:48 AM5/5/08
to
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 ?

foxidrive

unread,
May 5, 2008, 11:38:16 AM5/5/08
to
On Mon, 5 May 2008 07:58:48 -0700 (PDT), thinktwice <memor...@gmail.com>
wrote:

@echo off
setlocal EnableDelayedExpansion


set variable=oldvalue
for ... in (....) do (
set variable=newvalue

command1 !variable!
command2 !variable!
...
commandn !variable!)
set variable=oldvalue

Tom Lavedas

unread,
May 5, 2008, 11:41:24 AM5/5/08
to

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/

Pegasus (MVP)

unread,
May 5, 2008, 11:42:26 AM5/5/08
to

"thinktwice" <memor...@gmail.com> wrote in message
news:0fd49d53-8600-4f7d...@1g2000prg.googlegroups.com...

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!


thinktwice

unread,
May 5, 2008, 9:23:53 PM5/5/08
to
thanks guys, DelayedExpansion is really what i need, really
appreciated your help. :D

thinktwice

unread,
May 6, 2008, 3:34:23 AM5/6/08
to
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?

thinktwice

unread,
May 6, 2008, 4:10:38 AM5/6/08
to
findstr /i /s /c:"Error(s)" logdir\!projectname!.log

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

0 new messages