为什么这样code说反响了吗? [英] Why this code says echo is off?

查看:102
本文介绍了为什么这样code说反响了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是错的这个code,它说的回声是关闭的。

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

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

随着劳伦特说,这不是 ECHO 的问题,这是你的$ C $的问题角

As Laurent stated, it's not a problem of the ECHO, it's a problem of your code.

在批处理文件,块被执行之前,他们分析完成。结果
在解析所有%的扩张会做,如此看来,你的变量不能一个块中被改变。

In batch files, blocks are parsed complete 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.

这篇关于为什么这样code说反响了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆