具有for循环和管道的批处理脚本 [英] Batch script with for loop and pipe

查看:97
本文介绍了具有for循环和管道的批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望目录中的所有csv文件的文件名不包含单词"summary".在命令提示符下,我可以键入以下命令

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循环中不支持pipe命令.那是我不能做的事

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循环和管道的批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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