mirrormirror wrote:Does a FOR loop disable ECHO ON / OFF functionality, or "delay" it? ...
If there a way to turn on ECHO within for loops?
You actually have it sort of backwards
The behavior you are seeing has nothing to do with the FOR loop.
Statements are echoed while they are parsed, so ECHO must generally be ON
before parsing begins if you want to see the statements echoed. A block of statements involving parentheses and/or & is parsed all at once, so toggling ECHO ON and OFF within the block appears to have no effect.
Here are some examples without FOR loops that demonstrate the issue:
Code: Select all
@echo off
(
echo on
rem 1 - This is NOT echoed
echo off
)
echo on & rem.2 - This is NOT echoed & echo off
--NO OUTPUT--
But there is one exception that arises from the fact that the DO portion of a FOR loop has its own ECHO phase that fires an additional time for each iteration.
Note how the entire FOR statement is echoed once, and then the DO portion is echoed an additional two times:
Code: Select all
@echo on
for %%N in (1 2) do rem %%N
--OUTPUT--
Code: Select all
C:\test>for %N in (1 2) do rem %N
C:\test>rem 1
C:\test>rem 2
The ECHO state is checked before each iteration to see if the DO code should be echoed. We can use that fact to accomplish effective ECHO toggling within a block of statements by adding a dummy FOR loop
Code: Select all
@echo off
(
echo on
for %%. in (dummy) do rem 3 - This IS echoed
echo off
)
echo on & (for %%. in (dummy) do rem.4 - This IS echoed) & echo off
--OUTPUT--
Code: Select all
C:\test>rem 3 - This IS echoed
C:\test>rem.4 - This IS echoed
Here is the solution applied to your original code.
Code: Select all
@echo off & setlocal enableDelayedExpansion
for /f "tokens=1,2 delims=: " %%A in (
'ipconfig /all ^| jrepl "%srcIP%[\s\S]*?^[ \t]*?DNS Servers (?:\. )+:(([\s\r\n]+(\d+\.){3}\d+)+)" $1 /m /jmatch ^| findstr /n "^"'
) do (
echo on
for %%. in (dummy) do (
set "DNSServer.Cnt=%%A"
set "DNSServer.%%A=%%B"
)
echo off
)
Note how the ECHO ON/OFF statements are not within the inner loop, so you don't need the @ because they will not be echoed anyway
If you want to get rid of the echoed parentheses and odd spacing, then use a separate dummy FOR loop for each statement that you want to echo.
Code: Select all
@echo off & setlocal enableDelayedExpansion
for /f "tokens=1,2 delims=: " %%A in (
'ipconfig /all ^| jrepl "%srcIP%[\s\S]*?^[ \t]*?DNS Servers (?:\. )+:(([\s\r\n]+(\d+\.){3}\d+)+)" $1 /m /jmatch ^| findstr /n "^"'
) do (
echo on
for %%. in (dummy) do set "DNSServer.Cnt=%%A"
for %%. in (dummy) do set "DNSServer.%%A=%%B"
echo off
)
Dave Benham