My first attempt was a complete failure - thanks jeb for pointing out the errors. For those who are interested, the original answer is available in the edit history.
Aacini has a good solution if you don't mind putting your subroutine in a separate file.
Here is a solution that works without the need for a second batch file. And it actually works this time! :)
(Edit 2 - optimized code as suggested by jeb in the comment)
:mysub ::Silently get the echo state and turn echo off @( setlocal call :getEchoState echoState echo off ) ::Do whatever set return=returnValue ::Restore the echo state, pass the return value across endlocal, and return ( endlocal echo %echoState% set return=%return% exit /b ) :getEchoState echoStateVar @setlocal @set file=%time% @set file="%temp%\getEchoState%file::=_%_%random%.tmp" @( for %%A in (dummy) do rem ) >%file% @for %%A in (%file%) do @( endlocal if %%~zA equ 0 (set %~1=OFF) else set %~1=ON del %file% exit /b )
If you are willing to put up with a small risk of two processes at the same time, trying to access the same file, you can: simplify the procedure: getEchoState without the need to use SETLOCAL or the variable temp.
:getEchoState echoStateVar @( for %%A in (dummy) do rem ) >"%temp%\getEchoState.tmp" @for %%A in ("%temp%\getEchoState.tmp") do @( if %%~zA equ 0 (set %~1=OFF) else set %~1=ON del "%temp%\getEchoState.tmp" exit /b )
dbenham
source share