3

I use the below function to check if echo is set on or off, and as you can see it pipes echo statuse and then uses the output to set a flag:

FUNCTION

:echos 
echo > echo.txt & rem pipe ech status to a text file
@echo off
set _echos=yes
:: default assumption is that echo was on
CALL FileStringLength echo.txt & rem fetch string length. eq to 12 if off
IF %_strlength% EQU 12 (set _echos=no)
del echo.txt      & rem zap temporary file
set "_strlength=" & rem zap temporary variable
set "_echos"      & rem show status
IF %_echos% EQU yes (@echo on
) ELSE (
 @echo off
)
:: /echos

It works fine, but creating a file feels ... inelegant. so I thought, hmmm why not just do a redirection? replace the pipe with

>echo|set /p _str="redirect"

the result is as below, and when I check the string variable it's not there:

redirect
>set _str
Environment variable _str not defined
>

not what I wanted. At some point, in the early hours when the mind wanders I got the result! Whoohoo! crashed beautificaly on the sofa ... but could not for the life of me remember what arcane combination of code got the redirect to work! sigh anyone out there who has solved this particular problem? or was it all just a hallucination brought on by ocd in the early hours?

2
  • It's not possible. See stackoverflow.com/a/46324404/3536342 which explains why.
    – DavidPostill
    Commented Nov 18, 2019 at 22:42
  • thank you. Hallucination then! ... but at least there is a solution there. And at best I can get rid of that temp file jumping in and out of the folder :-)
    – ilinkcs
    Commented Nov 19, 2019 at 5:45

4 Answers 4

1

A function to test Echo state And assign State to Variable. Still couldn't avoid the temporary file sadly

@ECHO OFF

:getstate
CALL :ECHO_STATE
ECHO Echo is %state%
DEL /Q "tempEstate.sav"
PAUSE
GOTO :EOF

:ECHO_STATE
ECHO>tempEstate.sav && FIND /I "OFF" "tempEstate.sav" >nul || Set "state=on" && GOTO :EOF

Set "state=off"
GOTO :EOF
1
  • Thanks, TT3RROR, and sorry for not replying earlier - been on vac. Well, you have a more elegant solution, that's for sure! So it makes the prod code block smaller which in an interpretative engine is always a win in it's own right!
    – ilinkcs
    Commented Jan 11, 2020 at 9:11
1

I use the following to turn echo off for an established batch programme, so that I don't drown the programme I'm writing with its workings. With proper variable localisation (set and use %EchoEnable% within the same setlocal section), this will not overwrite the echo status memory for parent batch programmes. Unfortunately, this script still requires a temporary file.

  • The file name and extension should not be already used, or you will lose the original file.
  • My code suggests that the filename would be æ, but when I insert a pause in the code, the created file is called µ. The purpose is only to use a name you won't already have, and cmd reads the the file it just wrote before deleting it, so I do not expect any problem here. This may or may not be due to a mismatch of my code pages (I echoed that character from cmd).
  • Since the extension does not matter to cmd, I made up garble. A quick Google search fails to return anything about garble files, so I would assume it is unused.
  • echo NUL>æ.garble clears the file of data before it is deleted, so it is much harder to restore the deleted file (what does that matter for a file which says ECHO is on. [or off]?), so this command could be removed.
  • You must have write access for the current directory (in %cd%). Otherwise, an absolute directory coulld be specified for the file.
    :EchoDisable.bat
    @echo >æ.garble
    @echo off
    for /F "tokens=1,3 delims=. " %%F in ('type æ.garble') do set EchoEnable=%%F %%G
    echo NUL>æ.garble & DEL æ.garble

    ::restore echo setting with %EchoEnable%

Regarding the previous answers:
I get the same result as @ilinkcs for the for /f in ('echo') loop: always off. Maybe the command is run from another instance of cmd, and fed to for.exe?

If I am reading it correctly (like cmd would), @T3RROR's answer will not work, as echo is turned off before getting its state, so it will always be found to be off. I would avoid the call and goto :EOF since it can be done on one line, and then @echo off:

    ECHO>tempEstate.sav & @FIND /I "OFF" "tempEstate.sav" >nul && @Set "state=off" || @Set "state=on"
    @echo off
New contributor
Big Fox is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
6
  • @for /F "delims=" %%X in ('echo') do echo --for--: %%X should always show echo is on because the echo command is executed in a new cmd.exe child process and there it's ON by default
    – jeb
    Commented 2 days ago
  • Instead of looking for an unused extension you could just use tmp instead. It's the standard for temporary files
    – jeb
    Commented 2 days ago
  • @jeb echo, or any ('command')?
    – Big Fox
    Commented yesterday
  • @jeb Are there any guidelines for temporary file names? Kar.ma "improved" on your use of filename X by using æ (less common). Any idea why my cmd uses µ when I tell it to use æ? Finally, do you read T3RROR's code the way I do?
    – Big Fox
    Commented yesterday
  • I just noticed something: you use delayed expansion without enabling it, but the code works.
    – Big Fox
    Commented yesterday
0

The solution in the link @DavidPostill suggested fails in my use case, sadly.

for /f "delims=" %%a in ('echo 111') do set "readValue=%%a"

However when running the solution in a batch (to capture the status of echo):

@echo off
echo: pre for-do
echo
for /f "delims=" %%a in ('echo') do set "_str=%%a"
echo: post for-do
echo
echo _str= %_str%

the output is:

 pre for-do
ECHO is off.
 post for-do
ECHO is off.
_str= ECHO is on.

so the question, I guess remains unresolved. I still have the rather inelegant solution, so it's not all doom and gloom! If anyone else has some ideas, I'd be real happy to explore them :-)

0

It's possible to detect the echo state regardless of language.
The batch file creates a temporary file, if echo is OFF the file is empty else it contains something like C:\> REM.
The state can then be determined by the length of the file.

@echo off
echo Testing detectEchoState

echo on
@call :detectEchoState

@echo off
@call :detectEchoState off
exit /b


:detectEchoState
@(
    (
        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%
@exit /b

For a solution without a temporary file, see SO: Was ECHO ON or OFF when my Windows .bat was CALLed?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .