如何在除带有批处理文件的文件中列出的目录之外的大多数目录上执行代码?
因此,我尝试在另一个目录下的大多数目录上执行代码,除了我在文本文件中列出的目录之外。例如,如果我想直接在 C:\ 中的所有目录上运行代码,但不说 C:\avoidme\,我会在exclusions.txt 中添加 C:\avoidme 作为一行。但我下面的代码似乎不起作用。关于如何做到这一点有什么想法吗?
for /f %%d in ('dir /b C:\') do (
find /c "C:\%%d" exclusions.txt
if %errorlevel% equ 1 (
Do code here
)
So I am trying to execute code on most directories under another except for those that I list in a text file. So for example, if I wanted to run the code on all directories directly in C:\ but not say C:\avoidme\ I would add C:\avoidme as a line in exclusions.txt. But the code I have below does not seem to be working. Any ideas on how this could be done?
for /f %%d in ('dir /b C:\') do (
find /c "C:\%%d" exclusions.txt
if %errorlevel% equ 1 (
Do code here
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码不起作用的原因是在实际执行 FOR 语句之前立即解析了 FOR DO() 子句的整个主体。但是 %ERRORLEVEL% 的值在解析时会扩展,因此您永远不会在执行时看到更新的值。
您还有其他问题
您应该使用 DIR /AD 选项将列表仅限于目录
您不感兴趣查看 FIND 命令的输出,因此您可能应该将输出重定向到 nul
Givendkoa 有一种解决方案,可以工作。
另一种替代方法是使用延迟扩展。
这存在风险,如果 %%d 包含
!
,则它会在扩展时破坏该值。这可以通过切换循环内的延迟扩展来解决。另一种选择是使用
||
运算符,这意味着如果上一个命令失败,则执行以下命令。也许最好的选择是通过使用 /V 和 /G: 选项将 DIR 的结果直接通过管道传输到 FINDSTR 来完全消除对错误级别的担忧,以过滤掉排除列表中出现的值。
以下内容应该可以工作,
但是 - 有一个令人讨厌的 FINDSTR 错误,如果您搜索不同长度的多个文字字符串,则可能会导致它失败。
解决方法是强制 FINDSTR 使用正则表达式,但随后您需要转义可能出现在排除列表中的任何正则表达式元字符。例如,名为
myName.ext
的目录必须转义为myName\.ext
。需要在exclusion.list中转义的其他字符有
\
、^
、$
、[
、<代码>]The reason your code does not work is because the entire body of the FOR DO() clause is parsed at once prior to actually executing the FOR statement. But the value of %ERRORLEVEL% is expanded at parse time, so you never get to see the updated value at execution time.
You have additional issues
you should use the DIR /AD option to restrict the list to directories only
you aren't interested in seeing the output of your FIND command, so you should probably redirect the output to nul
gavendkoa has one solution that will work.
Another alternative is to use delayed expansion
This has a risk in that %%d will corrupt the value at expansion time if it contains
!
. This can be solved by toggling the delayed expansion within the loop.Another alternative is to use the
||
operator, which means execute the following command if the previous command failed.Perhaps the best option is to eliminate the need to worry about the errorlevel at all by piping the results of DIR directly to FINDSTR with the /V and /G: options to filter out values that appear in your exclude list.
The following is supposed to work
BUT - there is a nasty FINDSTR bug that can cause it to fail if you search for multiple literal strings of different lengths.
The fix is to force FINDSTR to use regular expressions instead, but then you need to escape any regular expression meta-characters that may appear in your exclusion list. For example, a directory named
myName.ext
would have to be escaped asmyName\.ext
.Other characters that would need to be escaped within exclusion.list are
\
,^
,$
,[
,]