Solved

Batch File Nested IF Part Deux

Posted on 2010-09-16
6
744 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 56

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 56

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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 56

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 56

Expert Comment

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

~bp
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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