Solved

Batch File Nested IF Part Deux

Posted on 2010-09-16
6
696 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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
Great Approach!
Once again spot on!
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Great, glad that helped, thank you.

~bp
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now