Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch File Nested IF Part Deux

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

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 59

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 59

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 59

Expert Comment

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

~bp
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

972 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