GOTO command inside a FOR loop

September 2, 2009 at 05:22:37
Specs: Windows 2000
I created a simple batch job that reads the third line in a file and sets the third line to a variable. the code works, it is:

@echo off
set rep=0
set report=0
for /F "usebackq delims=" %%r in ("%%a") do (
set /a rep=rep+1
if !rep!==3 (
set report=%%r
if !rep!==3 Goto :End
)
)
:End

The third line in the file is always the title, this works, but I want it to do it for every txt file in a folder, so I put it in a larger for loop:

@echo off
for /F "usebackq delims=\ " %%a in (`dir /b *.txt`) do if not "%%a"=="results.txt" (
echo ------------------
echo Parsing %%a
echo ------------------
pause
set rep=0
set report=0
for /F "usebackq delims=" %%r in ("%%a") do (
set /a rep=rep+1
if !rep!==3 (
set report=%%r
if !rep!==3 Call :End
)
)
:End
)


This doesn't run, if I take out the ":End". It will run, but obviously not work as intended. This could easily be written in any lanuage, but dos is all I know. I mean to learn VBS at some point. Any help is greatly appreciated


See More: GOTO command inside a FOR loop

Report •


#1
September 5, 2009 at 08:59:29
GOTO within a FOR is usually challenging.

==================================
@echo off > newfile & setLocal enableDELAYedexpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d *.txt') do (
set N=
for /f "tokens=* delims= " %%i in (%%a) do (
set /a N+=1
if !N! equ 3 echo %%i >> newfile
)
)


=====================================
Helping others achieve escape felicity

M2


Report •
Related Solutions


Ask Question