The main problem is, that fetching the current state doesn't work neither with FOR nor with pipes as both invoke a new cmd.exe instance where the echo state is resetted.
The current working solution is to redirect the output of phase3 and test if there was any output, then phase3 was active and echo must be ON.
Code: Select all
@(
(
FOR /F %%L in ("1") do REM
) > "%TEMP%\echoCheck.tmp"
FOR /F "delims=" %%F in ("%TEMP%\echoCheck.tmp") do @if %%~zF == 0 (set "EchoState=OFF" ) ELSE (set "EchoState=ON")
del "%TEMP%\echoCheck.tmp"
echo EchoState !EchoState!
)
Code: Select all
@(
for %%A in (1) do REM
) 1>&0
I could redirect the stderr to a file, but then it's not better than the originial solution.
Now, I combined the cursor back technic from Aacini with the phase3 output and it works.
Code: Select all
@echo off
setlocal EnableDelayedExpansion
cls
call :createChar 0x09 TAB
call :createChar 0x08 BS
REM Test1
echo off
@call :detectEchoState
@set echoState_test1=!echoState!
@echo off
REM Test2
@echo on
@call :detectEchoState
@set echoState_test2=!echoState!
@echo off
cls
echo EchoState_test1 !EchoState_test1!
echo EchoState_test2 !EchoState_test2!
exit /b
:detectEchoState
@cls
@set "echoState=ON"
REM
@for /F "delims=" %%L IN ('"((echo %%TAB%%%%BS%%%%BS%%) > con) 2>&1"') DO @set "echoState=OFF"
@exit /b
:createChar
(set LF=^
%=EMPTY=%
)
for /f eol^=^%LF%%LF%^ delims^= %%X in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(%~1"') do set "%~2=%%X"
exit /b
Then test if phase3 is active with a simple REM.
In the next step I try to go up one line with the TAB-BS-BS trick and redirect all errors to stdout.
The for loop will only get anything when an error occoured and that only happens when the cursor still was still at the home position.
That's all, obviously
This solution has still some draw backs.
- For the echo state detection the cursor has to been moved to the home position
- There is some output to the screen when "ECHO is ON", while testing the state
- This solution can't be used for detecting KEYS or VERIFY state, it only works for ECHO