Solved

Proper Syntax for Nested IF and FOR Statements

Posted on 2009-03-29
5
2,058 Views
Last Modified: 2012-05-06
I have a batch file with some nested FOR statements. In a Microsoft reference on XP command line code it says you need to preface nested statements with an "@", but I have yet to see any discussions on the Web with code using anything other than parentheses to indicate nesting.

What's the story? Is the "@" needed, and if so, does it need to be in the first column? What happens under the XP Pro SP2 command line if it is there versus if it is left out?

And if I have nested IF statements, do I really need it (like that Microsoft reference says)?

My own observations are inconclusive and I was hoping someone out there knows this obscure thing.
0
Comment
Question by:Ballstonian
[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
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:Quetzal
ID: 24013190
To what reference are you referring.  @ <command> causes command to be performed but not echoed to stdout.  If you are using proper syntax, neeted IF and FOR statements should not be a problem.
0
 

Author Comment

by:Ballstonian
ID: 24013421
See the section under Nesting IFs at http://windowscommand.uw.hu/wincmd0024.html as an example:
A nested if is an if statement within an if statement. Nested ifs are very common in programming, and command-shell programming is no exception. When you nest if statements, pay attention to the following points:

Use parentheses to define blocks of code and the @ symbol to designate the start of the nested if statement.

Remember that an else statement always refers to the nearest if statement that is within the same block as the else statement and that is not already associated with another else statement.

Here is an example: if "%1"=="1" (
@if "%2"=="2" (hostname & ver) else (ver)) else (hostname & ver & 
netstat -a)
 
The first else statement is associated with if "%2"=="2". The final else statement is associated with if "%1"=="1".
Just wondering if this is really true and if not, why do they have it in the reference?
0
 
LVL 11

Accepted Solution

by:
Quetzal earned 250 total points
ID: 24013486
The @ only suppresses echo of the command and is NOT required for nested IF syntax.  You can easily demonstrate this for yourself by typing a nested IF at the command prompt, eg.
if "A"=="A" (if "B"=="B" (echo B) else (echo C)) else echo not A  produces B
if "A"=="A" (if "B"=="C" (echo B) else (echo C)) else echo not A produces C
if "A"=="X" (if "B"=="B" (echo B) else (echo C)) else echo not A produces not A

@ not required for proper function.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24027013
Your example above expands to the following code:

if "%1"=="1" (
   if "%2"=="2" (
      hostname
      ver
   ) else (
      ver
   )
) else (
   hostname
   ver
   netstat -a
)


So, given the conditions where the following command line parameters are passed, the following will be performed:

(nothing)            hostname, ver, netstat -a
1                        ver
1 2                     hostname, ver
(anything else)  hostname, ver, netstat -a
0
 

Author Closing Comment

by:Ballstonian
ID: 31564046
But I still don't know why the Microsoft reference says you need the "@" symbol for nested statements!
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

726 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