带有 for 循环和管道的批处理脚本
我想要一个目录中的所有 csv 文件,其文件名不包含单词“summary”。在命令提示符中,我可以键入以下命令
dir /b my_dir\*.csv | find /V "summary"
当我尝试将上述命令传输到批处理文件中时,我遇到了一个问题,因为 for 循环中不支持管道命令。也就是说,我不能执行以下操作
FOR /f %%A in ('dir /b my_dir\*.csv | find /V "summary"') do (
rem want to do something here
)
有人可以告诉我如何解决上述问题吗?
提前致谢!
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"
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?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要转义
|
字符,以防止在解析循环命令时对其进行解释。。使用^
对其进行转义:一旦转义,
|
就成为'
分隔字符串的一部分。仅当根据语法将该字符串与循环分开解析时,它才被解释为特殊符号,作为“子命令”。这是在解析循环之后完成的。You need to escape the
|
character to prevent its being interpreted at the time of parsing the loop command. Use^
to escape it: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.如果您遇到 Gilbeg 出现“find: /V': No such file or directory”的问题,那么很可能您的路径中有 cygwin 或类似文件,并且批处理文件未使用 Windows find 命令。如果您修改脚本以使用 Windows 查找的绝对路径,则错误将消失:
If you get the problem that Gilbeg got "find: /V': No such file or directory" then it's most likely you have cygwin, or similar, in your path and the batch file's not using the Windows find command. If you modify your script to use the absolute path of the Windows find then the error will go away:
您还可以在单引号字符串中嵌入双引号字符串,如下所示:
You can also just embed a double-quoted string inside the single-quotes string, as in:
查看 Windows PowerShell。请注意,我自己并没有使用过它。
have a look at the Windows PowerShell. Not that I have ever used it myself, mind.