• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2074
  • Last Modified:

Proper Syntax for Nested IF and FOR Statements

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
Ballstonian
Asked:
Ballstonian
  • 2
  • 2
1 Solution
 
QuetzalCommented:
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
 
BallstonianAuthor Commented:
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
 
QuetzalCommented:
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
 
t0t0Commented:
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
 
BallstonianAuthor Commented:
But I still don't know why the Microsoft reference says you need the "@" symbol for nested statements!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now