简体   繁体   English

echo变量在批处理文件中不起作用

[英]echo variable is not working in batch file

My batch file execution throws error at echo echo %outfvar%. 我的批处理文件执行在echo echo%outfvar%处引发错误。 The following is the batch file I wrote: 以下是我编写的批处理文件:

setlocal ENABLEDELAYEDEXPANSION
set /a incvar = 1
set outfvar = "outfile"_!incvar!".res"
echo !outfvar!
echo *.txt > !outfvar!
set /a incvar = incvar+1

FOR %%pat in (%*) do(
    FOR /F %%k in (!outfvar!) DO( grep -l !pat! !k! >>outfile_!incvar!.res)
    set /a incvar = incvar+1
    set outfvar = "outfile"_!incvar!.res
                     )

Error is "%pat was unexpected at this time.." Can anybody help me to execute this batch file successfully? 错误是“此时%pat出乎意料。。”有人可以帮助我成功执行此批处理文件吗?

Remove the spaces around = in all set comands. 删除所有 set =周围的空格。

There must be a space in between do and ( in the line of for . do(for的行中)之间必须有一个空格。

The line 线

set outfvar = "outfile"_%incvar%".res"

should read 应该读

set "outfvar=outfile_%incvar%.res"

(The quotes as you stated them were part of the string value.) (您所说的引号是字符串值的一部分。)

for variables must consist of one letter only and need to be expanded by preceding with %% . for变量, 必须仅包含一个字母,并且需要在%%之前加上扩展名。 You are trying to use %%pat in your code, which will not work. 您正在尝试在代码中使用%%pat ,这将不起作用。 State %%p instead (also inner for ). 请改为使用%%p (也for )。

Finally, you need delayed expansion to be able to read variables you modify within the same (compound) command, the for in your code. 最后,您需要延迟扩展才能读取您在同一(复合)命令(代码中的for )中修改的变量。 See this post to learn how it works. 请参阅这篇文章以了解其工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM