MS Dos Command - For Loop and Goto

I am trying to use the for loop to read the the text file and return "TRUE" if equal to "ABC"

Text file
ABC      1
EFG      2

However, seems there is some problem if I use For loop and Goto command together
- Program stops after reading the first line
- Seems ok if I have removed "Goto" Command

Please help....Thanks



FOR /F "TOKENS=1-2 DELIMS=	" %%a in ('TYPE LIST.TXT') DO (
ECHO PREFIX -  %%a
ECHO SOURCE FILE - %%b
 
:ABC
REM ECHO RESULT = FALSE
 
IF %%a == ABC GOTO HIJ
REM IF %result%==TRUE GOTO EFG
REM IF %result%==FALSE GOTO HIJ
 
:EFG
ECHO TRUE
 
:HIJ
ECHO FALSE
)
PAUSE

Open in new window

hoikarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Don't use labels when grouping commands.
Try something like this:
set Result=FALSE
FOR /F "TOKENS=1-2 DELIMS=      " %%a in ('TYPE LIST.TXT') DO (
  ECHO PREFIX -  %%a
  ECHO SOURCE FILE - %%b
  IF /i "%%a"=="ABC" set Result=TRUE
)
echo %Result%
PAUSE

Open in new window

0
hoikarAuthor Commented:
That's mean I cannot terminate the for loop if I got result = "False", I need to loop until the EOF?
0
oBdACommented:
Not necessarily, you just can't put labels inside a grouped statement.
Something like this will work, too:
set Result=FALSE
FOR /F "TOKENS=1-2 DELIMS=      " %%a in ('TYPE LIST.TXT') DO (
  ECHO PREFIX -  %%a
  ECHO SOURCE FILE - %%b
  IF /i "%%a"=="ABC" (
    set Result=TRUE
    goto Done
  )
)
echo %Result%
PAUSE

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

AmazingTechCommented:
What about using FIND.
Set Result=FALSE
FOR /F "TOKENS=1-2 DELIMS=	" %%a in ('TYPE LIST.TXT ^| find /i "ABC"') do
ECHO PREFIX - %%a & ECHO SOURCE FILE - %%b & Set Result=TRUE
ECHO %Result%

Open in new window

0
hoikarAuthor Commented:
may I know which window version you are currently using? is it work on Win2000 as well?
0
oBdACommented:
Yes, that should even work on NT4.
Oh, and I just noticed that I forgot to put in the target label; just put a ":Done" below line 9:

set Result=FALSE
FOR /F "TOKENS=1-2 DELIMS=      " %%a in ('TYPE LIST.TXT') DO (
  ECHO PREFIX -  %%a
  ECHO SOURCE FILE - %%b
  IF /i "%%a"=="ABC" (
    set Result=TRUE
    goto Done
  )
)
:Done
echo %Result%
PAUSE

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AmazingTechCommented:
Hmm... I can see now how my example was confusing and didn't work because it somehow got split.

It should have been this.
Set Result=FALSE
FOR /F "TOKENS=1-2 DELIMS=	" %%a in ('TYPE LIST.TXT ^| find /i "ABC"') do ECHO PREFIX - %%a & ECHO SOURCE FILE - %%b & Set Result=TRUE
ECHO %Result%

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.