Find the ECHO state

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
WernerGg
Posts: 19
Joined: 06 May 2011 08:07
Location: Stuttgart, Germany, currently Riyadh, KSA

Find the ECHO state

#1 Post by WernerGg » 08 May 2011 05:17

During debugging with "echo on" I want to disable echo within uninteresting subfunctions and reestablish the original state later. I.e something like:

Code: Select all

call :EchoOff
:: do uninteresting things
call :EchoBack

I try:

Code: Select all

:EchoOff
:: Store current echo-state. Switch echo off
set $LEchoOn=off&for /f %%o in ('echo|find "(ON)"') do (set $LEchoOn=on)
echo off
goto :EOF

:EchoBack
:: Restore previous echo-state.
echo %$LEchoOn%
goto :EOF


But the echo|find combination does not work.

I try manually at the command promt (red my entries):
>echo
ECHO ist eingeschaltet (ON).

>echo | find "(ON)"
ECHO ist eingeschaltet (ON).

>echo off
echo
ECHO ist ausgeschaltet (OFF).
echo | find "(ON)"
ECHO ist eingeschaltet (ON).


I am stuck at the last line. echo is definitly off, but echo | find "(ON)" finds it ON.

??

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find the ECHO state

#2 Post by aGerman » 08 May 2011 06:24

Ha ha, you're right. ECHO and | result in awrong output. No idea why. Later I'll try to solve it. Currently I'm at work.

Regards
aGerman

WernerGg
Posts: 19
Joined: 06 May 2011 08:07
Location: Stuttgart, Germany, currently Riyadh, KSA

Re: Find the ECHO state

#3 Post by WernerGg » 08 May 2011 08:02

??? :roll:

This works:
>echo
ECHO ist eingeschaltet (ON).

>echo>e.tmp & find "(ON)" e.tmp

---------- E.TMP
ECHO ist eingeschaltet (ON).

>echo off
echo
ECHO ist ausgeschaltet (OFF).
echo>e.tmp & find "(ON)" e.tmp

---------- E.TMP


But I don't like that useless e.tmp file. And I must get rid of that first header line from find.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find the ECHO state

#4 Post by aGerman » 08 May 2011 08:16

Found no better way than redirecting the output to a file. Have a look at my solution:

Code: Select all

@echo off &setlocal
echo 1st Hello World
call :switchEchoState
echo 2nd Hello World
call :switchEchoState
echo 3rd Hello World

pause
goto :eof

:switchEchoState
@prompt $ &>"%temp%\state.tmp" echo
@echo off
for /f %%a in ('findstr /i "off" "%temp%\state.tmp"^&del "%temp%\state.tmp"') do (prompt &echo on &goto :eof)
prompt
goto :eof


Regards
aGerman

WernerGg
Posts: 19
Joined: 06 May 2011 08:07
Location: Stuttgart, Germany, currently Riyadh, KSA

Re: Find the ECHO state

#5 Post by WernerGg » 08 May 2011 10:17

OKI, so we seem to have a strange bug in echo piped to find.

I did not understand the prompt-game in your code. (Btw: another undocumented feature "prompt $").

I do not want to toggle echo-state, but
    - switch it temporarily off (or on) - normaly after entry into a subroutine
    - and then back to previous state. - normaly before exit from that subroutine

I do that with one simple variable $EchoState. To be perfect I needed a stack for echo-state instead. Then one could switch echo locally on or off during a call chain.

What I finally use now:

Code: Select all

rem ##----------------------------------------------------------
:EchoOnOff on/off
:: Store current echo-state. Switch echo on or off.
::   on/off : Desired new echo state
::The following does not work. Bug in echo|find
::@set $LEchoState=off&for /f %%o in ('echo^|find "(ON)"') do (set $LEchoState=on)
:: Determine current echo-state
@set $tmp="%temp%\_gd_echostate.tmp"
@echo >%$tmp%
@set $LEchoState=off&for /f %%a in ('findstr "(ON)" %$tmp%') do (@set $LEchoState=on)
@del %$tmp%
:: Switch echo on/off as requested
@echo %~1
::@echo EchoOnOff: echo now temporarily %~1
Exit /b

rem ##----------------------------------------------------------
:EchoBack
:: Restore previous echo-state as stored by EchoOnOff
@echo %$LEchoState%
::@echo EchoBack: echo now back to %$LEchoState%
Exit /b


Regards,
Werner

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Find the ECHO state

#6 Post by amel27 » 09 May 2011 04:34

WernerGg wrote:OKI, so we seem to have a strange bug in echo piped to find.
it is not bug, it is by disign
command to the left of a pipe run in separate CMD.EXE process, it is easy for checking up in task manager:

Code: Select all

@echo off
(
echo
pause>nul
)|find /v ""
Thus, echo display status of other CMD process, not current.

WernerGg
Posts: 19
Joined: 06 May 2011 08:07
Location: Stuttgart, Germany, currently Riyadh, KSA

Re: Find the ECHO state

#7 Post by WernerGg » 10 May 2011 00:32

@amel27
Aha! Thank you for that explanation. I did not know that but it is clear now.

So the trick would be to somehow capture the stdout from "echo" without starting a new command processor.

Post Reply