windows 为什么这段代码说回声已关闭?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14334850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why this code says echo is off?
提问by user1979801
What is wrong with this code? It says ECHO is off
.
这段代码有什么问题?它说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.
任何帮助表示赞赏。
回答by laurent
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.
如果您的变量在某处为空,则它与单独使用“echo”命令相同,它只会打印 echo 的状态。
To avoid this, you should replace all your echo
commands with something like this:
为避免这种情况,您应该将所有echo
命令替换为以下内容:
echo var2: %var2%
That way, if %var2%
is empty it will just print "echo var2:" instead of "echo off".
这样,如果%var2%
为空,它将只打印“echo var2:”而不是“echo off”。
回答by jeb
As Laurent stated, it's not a problem of the ECHO
, it's a problem of your code.
正如 Laurent 所说,这不是 的问题ECHO
,而是您的代码的问题。
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.
我在这里使用了构造echo(
而不是echo
因为这将确保即使变量为空也能回显空行。
回答by geisterfurz007
Not sure, if this post is still read, but nevertheless.
You should try the following:
On top of the code right after @echo off
you have to put in
不确定,如果这篇文章仍然被阅读,但仍然如此。您应该尝试以下操作: 在@echo off
您必须输入后立即在代码之上
setlocal enabledelayedexpansion
Additionally anywhere you want to use variables changed in a block of brackets (like For-Loops
or If's
) you have to change the %
into !
to get
!varname!
此外任何你想用括号块(如更改的变量For-Loops
或If's
),你必须改变%
为!
以获得
!varname!
This should be helping...
这应该有帮助...
Greetings
你好
geisterfurz007
geisterfurz007
回答by jfajunior
The solution for your problem is to put the "echo"s after the if block is completed. Try this:
您的问题的解决方案是在 if 块完成后放置“回声”。尝试这个:
@ECHO off
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
set var1=%var2:~5,3%
goto print
) else (
echo error
goto result
)
:print
echo %var2%
echo %var1% > test.txt
echo %var1%
:result
del result
pause
This way you can see the solution as you wanted. Cheers! ;]
这样您就可以根据需要查看解决方案。干杯! ;]
回答by Manoj Purohit
First create a file a.txt in the same directory u have this batch file ... write some text in that...Note: only Windows 2000
Windows ME
Windows XP
Windows Vista
Windows 7 supports FINDSTR
首先在同一个目录下创建一个文件 a.txt 你有这个批处理文件......在里面写一些文本......注意:只有 Windows 2000 Windows ME Windows XP Windows Vista Windows 7 支持 FINDSTR
set /p pattern=Enter id:
findstr %pattern% a.txt > __query.tmp
set /p result=<__query.tmp
if %errorlevel%==0 (
set var2= %result%
echo %var2%
set var1= %var2:~5,3%
echo %var1% > test.txt
echo %var1%
) else (
echo error
)
del __query.tmp
pause
run this bath file .. you will find a substring(start=5,length=3) of the first line of string you have in a.txt in a newly created file test.txt. Finally got it working !
运行这个bath文件..你会在新创建的文件test.txt中找到a.txt中第一行字符串的子字符串(start=5,length=3)。终于让它工作了!