Solved

Proper Syntax for Nested IF and FOR Statements

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

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 …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

912 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

17 Experts available now in Live!

Get 1:1 Help Now