带有 for 循环和管道的批处理脚本

发布于 2024-11-07 20:12:48 字数 359 浏览 0 评论 0原文

我想要一个目录中的所有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事灯 2024-11-14 20:12:48

您需要转义 | 字符,以防止在解析循环命令时对其进行解释。。使用 ^ 对其进行转义:

FOR /f "delims=" %%A in ('dir /b "my_dir\*.csv" ^| find /V "summary"') do (
rem do what you want with %%A here
)

一旦转义,| 就成为 ' 分隔字符串的一部分。仅当根据语法将该字符串与循环分开解析时,它才被解释为特殊符号,作为“子命令”。这是在解析循环之后完成的。

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.

旧故 2024-11-14 20:12:48

如果您遇到 Gilbeg 出现“find: /V': No such file or directory”的问题,那么很可能您的路径中有 cygwin 或类似文件,并且批处理文件未使用 Windows find 命令。如果您修改脚本以使用 Windows 查找的绝对路径,则错误将消失:

FOR /f "delims=" %%A in ('dir /b "my_dir\*.csv" ^| %SYSTEMROOT%\system32\find.exe /V "summary"') do (
rem want to do something here with %%A
)

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:

FOR /f "delims=" %%A in ('dir /b "my_dir\*.csv" ^| %SYSTEMROOT%\system32\find.exe /V "summary"') do (
rem want to do something here with %%A
)
岁月染过的梦 2024-11-14 20:12:48

您还可以在单​​引号字符串中嵌入双引号字符串,如下所示:

FOR /f "delims=" %%A in ('"dir /b my_dir\*.csv | find /I /V "summary""') do @(
    ECHO Do something with "%%A"
)

You can also just embed a double-quoted string inside the single-quotes string, as in:

FOR /f "delims=" %%A in ('"dir /b my_dir\*.csv | find /I /V "summary""') do @(
    ECHO Do something with "%%A"
)
风吹过旳痕迹 2024-11-14 20:12:48

查看 Windows PowerShell。请注意,我自己并没有使用过它。

have a look at the Windows PowerShell. Not that I have ever used it myself, mind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文