Clarification of nested IF and FOR statements

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
findstr
Posts: 17
Joined: 09 Jun 2021 12:36

Clarification of nested IF and FOR statements

#1 Post by findstr » 04 Jul 2021 12:49

Hello!
I'm reading the book "The personal trainer: Windows command line".
It says that I need to use @ symbol to begin nested IF statement.
Use @ symbol to designate the start of the nested if statement.

Code: Select all

if "%1"=="1" ( 
    @if "%2"=="2" (hostname & ver) else (ver)
) else (
    hostname & ver & netstat -a
)
They also do the same with FOR statement:

Code: Select all

for /d %%B in (%APPDATA% %APPDATA%\*) do ( 
    @for %%C in ("%%B\*.txt") do (echo %%C)
)
I saw many examples on the Internet, where nested IF and FOR statements are used without @ symbol.
Do I need to use @ symbol to begin nested IF or FOR statement?
What is the difference between using @ symbol and not using it?
Thanks!

findstr
Posts: 17
Joined: 09 Jun 2021 12:36

Re: Clarification of nested IF and FOR statements

#2 Post by findstr » 04 Jul 2021 13:09

Also, what's the difference between goto LABEL and goto :LABEL?
Is is better to use : symbol with the name of the label?

aGerman
Expert
Posts: 4195
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Clarification of nested IF and FOR statements

#3 Post by aGerman » 04 Jul 2021 13:56

findstr wrote:
04 Jul 2021 12:49
It says that I need to use @ symbol to begin nested IF statement.
Use @ symbol to designate the start of the nested if statement.
I never heard that @ "designates" something. The @ symbol prevents the prompt from getting displayed.
In a Batch script it's often used to suppress the prompt of the very first command which turns the prompt off for the rest of the script, namely "echo off".
In command line mode (rather than script mode) you may use it to suppress the repeated prompt for every command executed in a loop.

Code: Select all

for /l %i in (1,1,3) do @echo %i

Is is better to use : symbol with the name of the label?
It doesn't make any difference, except for the virtual :EOF label which requires the colon.

Steffen

Post Reply