Solved

Batch File Nested IF

Posted on 2010-09-16
5
1,334 Views
Last Modified: 2012-05-10
I am trying to add the last bolded ELSE . . . to this batch code without success.

I have tested both ELSE as good but can not add them together.

How do I get FOR /F to see multiple nested IF Statements?

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
  • 3
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 33693991
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
 
LVL 9

Author Closing Comment

by:tonydemarco
ID: 33694094
Excellent!
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33694322
Thanks, glad that helped.

~p
0
 
LVL 9

Author Comment

by:tonydemarco
ID: 33695224
Wanting to Add:
IF "%%A"=="N3" (ECHO %%B >> "%OutputFile%"
to your solution below and should I be looking at an easier way to add to the search criteria?

Putting this in a new Question to follow . . .

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
 
LVL 9

Author Comment

by:tonydemarco
ID: 33695507
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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