Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch File Nested IF Part Deux

Posted on 2010-09-16
6
Medium Priority
?
750 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 57

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 57

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 57

Accepted Solution

by:
Bill Prew earned 2000 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 57

Expert Comment

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

~bp
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

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…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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