?
Solved

MS Dos Command - For Loop and Goto

Posted on 2008-11-05
7
Medium Priority
?
5,841 Views
Last Modified: 2012-08-14
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

0
Comment
Question by:hoikar
  • 3
  • 2
  • 2
7 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 22893752
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
 

Author Comment

by:hoikar
ID: 22901303
That's mean I cannot terminate the for loop if I got result = "False", I need to loop until the EOF?
0
 
LVL 85

Expert Comment

by:oBdA
ID: 22902533
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 21

Expert Comment

by:AmazingTech
ID: 22902580
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
 

Author Comment

by:hoikar
ID: 22902644
may I know which window version you are currently using? is it work on Win2000 as well?
0
 
LVL 85

Accepted Solution

by:
oBdA earned 500 total points
ID: 22902668
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
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23991339
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question