using goto command inside for loop

Microsoft Windows xp professional w/serv...
April 26, 2010 at 00:32:41
Specs: Windows XP
hi..
i am trying to create a batch file which will copy the first line of a file from a list of file to a temporary file with an extra field. but when i am using a goto command inside a for loop its giving error.

the code is as below
@echo off
setLocal EnableDelayedExpansion
dir /b/a-d c:\my_folder > c:\rfile.txt

for /f %%i in (c:\rfile.txt) do (
for /f "delims=$" %%j in (c:\my_folder\%%i) do (
echo %%j,filename >> c:\my_folder\temp_%%i
goto fin
)
:fin
)

can anybody tell me how to do this??

thanks in advance
roy


See More: using goto command inside for loop

Report •


#1
April 26, 2010 at 01:00:18
weirdism of neuvo-batch: no colon/labels next to ")"
move :fin outside the last ")", or do else to make it not
right next to the ")". No clue as to the reason... question NOT the almighty MicroSoft!!!
::------
for /f %%i in (c:\rfile.txt) do (
for /f "delims=$" %%j in (c:\my_folder\%%i) do (
echo %%j,filename >> c:\my_folder\temp_%%i
goto fin
)
)
:fin

Report •

#2
April 26, 2010 at 01:43:31
thanks for replying..
this solution will remove the error but not fulfill my requirement.
i am trying to use the goto statement as a brake statement like in c/c++ etc in the inner "for" loop.

i have tried by not putting the ":fin" label next to a ")". it has removed the error but instead of breaking out of the inner loop its coming out of the whole nested loop.

here is the modified code:

@echo off
setLocal EnableDelayedExpansion
dir /b/a-d c:\my_folder > c:\rfile.txt

for /f %%i in (c:\rfile.txt) do (
for /f "delims=$" %%j in (c:\my_folder\%%i) do (
echo %%j,filename >> c:\my_folder\temp_%%i
goto fin
)
echo >nul
:fin
echo >nul
)

any clue??

thanks


Report •

#3
April 26, 2010 at 06:19:46
And in C/C++, we'd name what you're doing undefined behavior.

Your best bet would be to use a sub function to split the loops:

@echo off
for /l %%a in (1 1 10) do call :loop %%a
goto :eof

:loop
for /l %%b in (1 1 10) do (echo %1 - %%b
goto :eof)


Report •

Related Solutions

#4
April 26, 2010 at 16:49:37
since you only want the first line of the first file:
@echo off & setlocal
for /f %%a in ('dir /b /a-d c:\my_folder') (
set /p lineone=<%%a
set aa=%%a
goto :xit
)
::-------
::or, if you really have to have the $ delimiter:
for /f %%a in ('dir /b /a-d c:\my_folder') do (
for /f "delims=$" %%b in (%%a) do (
set aa=%%a
set lineone=%%b
goto :xit
)
)
:xit
>> c:\myfolder\temp_%aa% echo %lineone%,filename


Report •

#5
April 27, 2010 at 00:00:55
actually i wanted the first line of all the files from the list to another separate file..

and this is how i have done it..

@echo off
setLocal EnableDelayedExpansion
echo dummyLine>c:\rfile.txt
dir /b/a-d c:\my_folder >> c:\rfile.txt
set /a count=1
:nextfile
for /f "skip=%count%" %%i in (c:\rfile.txt) do (
for /f "delims=$" %%j in (c:\my_folder\%%i) do (
echo filename,%%j >> c:\my_folder\temp_%%i
goto fin
)
echo >nul
:fin
set /a count+=1
goto nextfile
echo >nul
)

del c:\rfile.txt

the "skip" doesn't take 0 as a parameter that's why i put a dummyline in the list file at the beginning.

this script works fine but i think there may be a more efficient way to do this. any ways, now atlest i can go ahead with my project :-).

thanks for all the help guys
all the best..


Report •

Ask Question