Computing.Net > Forums > Windows 2000 > GOTO command inside a FOR loop

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

GOTO command inside a FOR loop

Reply to Message Icon

Name: naradlov
Date: September 2, 2009 at 05:22:37 Pacific
OS: Windows 2000
Subcategory: Configurations
Tags: DOS, for loop, batch, GOTO
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: September 5, 2009 at 08:59:29 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More


unable to install files f... windows protection prog e...


Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: GOTO command inside a FOR loop

BATCH FILE: loop inside a for loop www.computing.net/answers/windows-2000/batch-file-loop-inside-a-for-loop/65112.html

FOR command problem calling batch f www.computing.net/answers/windows-2000/for-command-problem-calling-batch-f/36946.html

variable inside for-loop( batch ) www.computing.net/answers/windows-2000/variable-inside-forloop-batch-/63092.html