I have the following script:
FOR %%i IN (1 2 3) DO (
IF %%i==1 (
ECHO %%i
)
IF %%i==2 (
ECHO %%i
)
IF %%i==3 (
ECHO %%i
)
)
I just wanted to print
1
2
3
because I will be using the same logic again to write a more complete task ... I'm not a Windows guy, and I have no idea how to do it in batch
.
Instead I'm getting:
c:\>FOR %i IN (1 2 3) DO (
IF %i == 1 (ECHO %i )
IF %i == 2 (ECHO %i )
IF %i == 3 (ECHO %i )
)
c:\>(
IF 1 == 1 (ECHO 1 )
IF 1 == 2 (ECHO 1 )
IF 1 == 3 (ECHO 1 )
)
1
c:\>(
IF 2 == 1 (ECHO 2 )
IF 2 == 2 (ECHO 2 )
IF 2 == 3 (ECHO 2 )
)
2
c:\>(
IF 3 == 1 (ECHO 3 )
IF 3 == 2 (ECHO 3 )
IF 3 == 3 (ECHO 3 )
)
3
The only way to stop an infinitely loop in Windows Batch Script is by either pressing Ctrl + C or by closing the program.
Edit your bat file by right clicking on it and select “Edit” from the list. Your file will open in notepad. Now add “PAUSE” word at the end of your bat file. This will keep the Command Prompt window open until you do not press any key.
batch-file Echo @Echo off @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.
Short answer: yes, batch files can modify themselves whilst running.
To avoid echoing the Windows commands in a batch file, use @echo off
:
@ECHO OFF
FOR %%i IN (1 2 3) DO (
IF %%i==1 (
ECHO %%i
)
IF %%i==2 (
ECHO %%i
)
IF %%i==3 (
ECHO %%i
)
)
Note the preceding @
in echo off
prevents echo off
from echoing. If you don't have the @
then you'll see echo off
echoed to the terminal but then echoing will be off after that point. A batch file command which is preceded by @
is not echoed. So @
can be used to prevent echoing of individual commands.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With