问题描述
我想要文件名不包含摘要"一词的目录中的所有 csv 文件.在命令提示符中,我可以输入以下命令
I would like all the csv files in a directory which filename does not contain word "summary". Inside the command prompt I can type the following command
dir /b my_dir*.csv | find /V "summary"
当我尝试将上述命令传输到批处理文件中时,我遇到了一个问题,即 for 循环中不支持管道命令.那就是我不能做以下事情
When I try to transfer the above command into a batch file I run into a problem in that the pipe command is not supported in the for loop. That is I cannot do the following
FOR /f %%A in ('dir /b my_dir*.csv | find /V "summary"') do (
rem want to do something here
)
有人可以告诉我如何解决上述问题吗?
Can somebody shed some light to me on how to solve the problem above?
提前致谢!
推荐答案
您需要对 |
字符进行转义以防止其在 解析循环命令时被解释.使用 ^
对其进行转义:
You need to escape the |
character to prevent its being interpreted at the time of parsing the loop command. Use ^
to escape it:
FOR /f "delims=" %%A in ('dir /b "my_dir*.csv" ^| find /V "summary"') do (
rem do what you want with %%A here
)
一旦转义,|
将成为 '
分隔字符串的一部分.根据语法,当该字符串与循环分开解析时,它仅被解释为特殊符号,作为子命令".这是在解析循环之后完成的.
Once escaped, the |
becomes part of the '
-delimited string. It is only interpreted as a special symbol when that string is parsed separately from the loop, as a "sub-command", according to the syntax. And that is done after parsing the loop.
这篇关于带有 for 循环和管道的批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!