As you have discovered, the changed ECHO state is not recognized by the parser until it reaches the instruction that is located after the code block that contains ECHO ON / OFF.
But there is one exception - commands after FOR ... DO accept a state change inside the same block :-)
Note that you only need @ to suppress the output of the command when ECHO is currently on. If it is off then there is no need for @ECHO ON . And if you turn it on and off in the same block of code, then you don't need that either.
Here is a demo that shows even test lines:
@echo off echo Test #1 ( echo on for %%. in (.) do echo Test #2 echo off echo Test #3 echo on for %%. in (.) do echo Test #4 echo off echo Test #5 ) echo on echo Test #6 @echo off echo Test #7
- OUTPUT -
Test #1 C:\test>echo Test #2 Test #2 Test #3 C:\test>echo Test #4 Test #4 Test #5 C:\test>echo Test #6 Test #6 Test #7
You may find it convenient to declare a simple echo_on macro. The result is the same result:
@echo off setlocal set "echo_on=echo on&for %%. in (.) do" echo Test
source share