Solved

Proper Syntax for Nested IF and FOR Statements

Posted on 2009-03-29
5
2,051 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Issue: Unstable cursor in Windows XP and Windows runs extremely slow in that any click will bring up the Hour glass (sometimes for several seconds before giving you what you want) . Troubleshooting Process and the FINAL FIX: This issue see…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

11 Experts available now in Live!

Get 1:1 Help Now