What is wrong with this code? It says ECHO is off
.
@ECHO off set /p pattern=Enter id: findstr %pattern% .\a.txt > result if %errorlevel%==0 ( set var2= <result echo %var2% set var1=%var2:~5,3% echo %var1% > test.txt echo %var1% ) else ( echo error ) del result pause
Any help is appreciated.
If your variable is empty somewhere, it will be the same as having the command "echo" on its own, which will just print the status of echo. That way, if %var2% is empty it will just print "echo var2:" instead of "echo off".
When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: Copy. @echo off.
The ECHO-ON and ECHO-OFF commands are used to enable and disable the echoing, or displaying on the screen, of characters entered at the keyboard. If echoing is disabled, input will not appear on the terminal screen as it is typed. By default, echoing is enabled.
The echo command ends with a line break by default. To suppress this, you can set the control character \c at the end of the respective output.
If your variable is empty somewhere, it will be the same as having the command "echo" on its own, which will just print the status of echo.
To avoid this, you should replace all your echo
commands with something like this:
echo var2: %var2%
That way, if %var2%
is empty it will just print "echo var2:" instead of "echo off".
As Laurent stated, it's not a problem of the ECHO
, it's a problem of your code.
In batch files, blocks are completely parsed before they are executed.
While parsing, all percent expansion will be done, so it seems that your variables can't be changed inside a block.
But for this exists the delayed expansion, the delayed expansion will be evaluated in the moment of execution not while parsing the block.
It must be enabled, as per default the delayed expansion is disabled.
@ECHO off setlocal EnableDelayedExpansion set /p pattern=Enter id: findstr %pattern% .\a.txt > result if %errorlevel%==0 ( set var2= <result echo(!var2! set var1=!var2:~5,3! echo(!var1! > test.txt echo(!var1! ) else ( echo error ) del result
I used here the construct echo(
instead of echo
as this will ensure echoing an empty line even if the variable is empty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With