Solved

Batch File Nested IF Part Deux

Posted on 2010-09-16
6
726 Views
Last Modified: 2012-05-10
Wanting to Add:

IF "%%A"=="N3" (ECHO %%B >> "%OutputFile%"

to solution below and should I be looking at an easier way to add to the search criteria?

FOR /F "Tokens=1-3,* Delims=^*" %%A IN ('Type "%SourceFile%"') DO (
  IF "%%A%%B"=="NM1IL" (
    ECHO %%D >> "%OutputFile%"
  ) ELSE (
    IF "%%A%%B"=="NM185" (
      ECHO %%D >> "%OutputFile%"
    ) ELSE (
      IF "%%A%%B"=="REFD9" ECHO %%C >> "%OutputFile%"
    )
  )
)
0
Comment
Question by:tonydemarco
  • 4
  • 2
6 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33695876
Can you share a sample of your input file.  It appears that what you are trying to do is only output a part of certain lines that match a list of items, is that correct?  There are a few ways we can approach this, but seeing the data would help.

~bp
0
 
LVL 9

Author Comment

by:tonydemarco
ID: 33696346
Sure. Sorry for the redacting but this is close enough.

BHT
*0019*00*000000005*20100901*2250*CH
REF*87*004010X098A1
NM1*41*2*WEBMD*****46*133052274
PER*IC*WEBMD CUSTOMER SOLUTIONS*TE*8008456592
NM1*40*2*PUGET SOUND HEALT*****46*421720001
HL*1**20*1
NM1*85*2*DANA CXXXXHAN PT*****XX*1962593871
N3*6659 KIMBXXX DR. STE A-101
N4*GIG XXXXXX*WA*98XXX
REF*EI*061694886
REF*G5*AA01
REF*G2*061694886
HL*2*1*22*0
SBR*P*18*******CI
NM1*IL*1*PAGE*HIGARD****MI*P0036101
N3*95 WOOD AVE
N4*GIG XXXXXX*WA*98XXX
DMG*D8*19330916*F
NM1*PR*2*PUGET SOUND HEALTH PARTNERS*****PI*42172
REF*FY*NOCD
CLM*275616-1*290***11::1*Y*A*Y*Y*B
REF*G1*2010060903000003
REF*D9*090110786959925
NTE*ADD*PROGRESS NOTE 62410-8 MORE VISITS
HI*BK:71596*BF:71946
NM1*DN*1*KHAN*WAHAT*A***XX*10970172
NM1*82*1*CXXXXHAN*DANA*M**PT*XX*1952492852
PRV*PE*ZZ*225100000X
NM1*FA*2*SOURCE THERAPY DANA CXXXXHAN PT
N3*665 KIMXXX DR STE 1-101
N4*GIG XXXXXX*WA*983XX5138
LX*1
SV1*HC:97110*50*UN*1***1:2
DTP*472*D8*20100824
REF*G1*2010060903000003
REF*6R*275616-20100831-0853
LX*2
SV1*HC:97112:59*40*UN*1***1:2
DTP*472*D8*20100824
REF*G1*2010060903000003
REF*6R*275616-20100831-0853
LX*3
SV1*HC:97530:59*55*UN*1***1:2
DTP*472*D8*20100824
REF*G1*2010060903000003
REF*6R*275616-20100831-0853
LX*4
SV1*HC:97110*50*UN*1***1:2
DTP*472*D8*20100826
REF*G1*2010060903000003
REF*6R*275616-20100831-1111
LX*5
SV1*HC:97112:59*40*UN*1***1:2
DTP*472*D8*20100826
REF*G1*2010060903000003
REF*6R*275616-20100831-1111
LX*6
SV1*HC:97530:59*55*UN*1***1:2
DTP*472*D8*20100826
REF*G1*2010060903000003
REF*6R*275616-20100831-1111
SE*63*000000005
ST*837*000000006


CURRENT OUTPUT:
DANA CXXXXHAN PT*****XX*1962593871
PAGE*HIGARD****MI*P0036101
090110786959925

WOULD LIKE:
DANA CXXXXHAN PT*****XX*1962593871
6659 KIMXXX DR. STE A-101
GIG XXXXXX*WA*98XXX
PAGE*HIGARD****MI*P0036101
090110786959925



0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33696689
Since both the value that you are matching on, as well as the part you want to write out can vary, I think this may be the simplest and easiest to maintain approach.  I played around with a few more clever approaches, but I don't think they end up being any better than this.

~bp
FOR /F "Tokens=1-3,* Delims=^*" %%A IN ('Type "%SourceFile%"') DO (
  IF "%%A%%B"=="NM1IL" ECHO %%D>>"%OutputFile%"
  IF "%%A%%B"=="NM185" ECHO %%D>>"%OutputFile%"
  IF "%%A%%B"=="REFD9" ECHO %%C>>"%OutputFile%"
  IF "%%A"   =="N3"    ECHO %%B>>"%OutputFile%"
)

Open in new window

0
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.

 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 33696711
I'll point out that the above approach doesn't use the ELSE structure, and as a result will execute each IF statement for all records.  But as I looked at your data, even with the else approach that would be true as well, and since the number of records being read in is not huge then this feels like the right approach to me.

~bp
0
 
LVL 9

Author Closing Comment

by:tonydemarco
ID: 33696785
Great Approach!
Once again spot on!
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33696891
Great, glad that helped, thank you.

~bp
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Remote Application Install 5 75
Backup solution 14 78
Use of delims in For /f loop in a batch file 4 112
Help with a batch file 13 83
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

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