与环和管道DOS批处理脚本 [英] DOS batch script with for loop and pipe
问题描述
我想在文件名不包含单词摘要目录中的所有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格式。|找到/ V摘要
当我尝试了上面的命令传输到一个批处理文件,我碰上了管道命令一个问题,就是不能在支持循环。这是我不能做以下
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?
在此先感谢!
推荐答案
您需要躲避 |
字符prevent它是间$ P $在PTED时间的解析循环命令的。使用 ^
来逃避它:
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
)
一旦逃脱,在 |
变成了
-delimited字符串的一部分。它只是PTED作为特殊符号时该字符串被从环路分别解析,作为子命令,根据语法间$ P $。而做到这一点的之后的解析循环。
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.
这篇关于与环和管道DOS批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!