How do I exit from the for loop?

July 16, 2009 at 09:08:00
Specs: Windows XP
rem ---------- compare .c file list with .o list ---------------

for /f "tokens=* delims=" %%a in (c_list.txt) do (
set pathname1=%%~dpa
set filename1=%%~na
set notfound=1
set flag=1

for /f "tokens=* delims= " %%b in (o_list.txt) do (
set pathname2=%%~dpb
set filename2=%%~nb

if !filename1! equ !filename2! (
set notfound=0
)

)

if !flag! equ !notfound! (
echo !pathname1!!filename1!.c is not found >> result.txt
)

)

This is my program. Now If I find the c file name in o_file list then I want to exit from for loop how do I do that?


See More: How do I exit from the for loop?

Report •


#1
July 16, 2009 at 09:16:33
You can only exit from a for loop with a goto.

Instead of an inner for loop, you might want to consider using
FIND, and the whole loop (and goto and label) is replaced by one
line:

find "!filename1!.o" o_list.txt || echo !filename1! not found.


Report •

#2
July 16, 2009 at 13:12:02
Thank you very much klint.

Even thought echo "off" the find command echos to window. Why?


Report •

#3
July 16, 2009 at 14:36:44
Why is the "goto" not working in this program?
I want to get out from the 2nd loop if the file name matched with the object list name.

Any help??


@echo off
SetLocal EnableDelayedExpansion


rem ---------- compare .c file list with .o list ---------------

for /f "tokens=* delims=" %%a in (c-files.txt) do (
set cpathname=%%~dpa
set cfilename=%%~na
set notfound=1
set flag=1

rem find "!cfilename!.o" o_list.txt || set notfound=0

for /f "tokens=* delims= " %%b in (obj-list.txt) do (
set opathname1=%%~dpb
set ofilename1=%%~nb

if !cfilename! equ !ofilename1! (
set notfound=0
goto Againc
)

)

Againc
if !flag! equ !notfound! (
echo !cpathname!!cfilename!.c is not found >> resultc.txt
)

)


Report •

Related Solutions

#4
July 17, 2009 at 02:26:51
The find command produces output due to an oversight on my
part. The echo off command only affects the echoing of
commands being executed. To suppress FIND's output, you
need to redirect it to the null device, so my original suggestion
should have been:

find "!filename1!.o" o_list.txt >nul || echo !filename1! not found.

The goto in your code is not working because labels must be preceded by : (a colon.)


Report •

#5
July 17, 2009 at 09:09:23
Thankyou very much klint. I really appriciate it.

No. I put the : (colon) in my program but it didn't work, something wrong in my logig. I forget or missed the : here when I cut & paste.
Anyway you solution (find) is great. Thanks again


Report •


Ask Question