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
Solved

Proper Syntax for Nested IF and FOR Statements

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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