Page 1 of 1

Clarification of nested IF and FOR statements

Posted: 04 Jul 2021 12:49
by findstr
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!

Re: Clarification of nested IF and FOR statements

Posted: 04 Jul 2021 13:09
by findstr
Also, what's the difference between goto LABEL and goto :LABEL?
Is is better to use : symbol with the name of the label?

Re: Clarification of nested IF and FOR statements

Posted: 04 Jul 2021 13:56
by aGerman
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