如何在除带有批处理文件的文件中列出的目录之外的大多数目录上执行代码?

发布于 2025-01-03 11:27:04 字数 297 浏览 2 评论 0原文

因此,我尝试在另一个目录下的大多数目录上执行代码,除了我在文本文件中列出的目录之外。例如,如果我想直接在 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 技术交流群。

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

发布评论

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

评论(2

夜空下最亮的亮点 2025-01-10 11:27:04
@echo off
for /D %%d in (*) do (
    %WINDIR%\system32\find "%%d" exclude.list >nul
    if errorlevel 1 (
        echo good %%d
    ) else (
        echo bad %%d
    )
)
@echo off
for /D %%d in (*) do (
    %WINDIR%\system32\find "%%d" exclude.list >nul
    if errorlevel 1 (
        echo good %%d
    ) else (
        echo bad %%d
    )
)
迷你仙 2025-01-10 11:27:04

您的代码不起作用的原因是在实际执行 FOR 语句之前立即解析了 FOR DO() 子句的整个主体。但是 %ERRORLEVEL% 的值在解析时会扩展,因此您永远不会在执行时看到更新的值。

您还有其他问题

  • 您应该使用 DIR /AD 选项将列表仅限于目录

  • 您不感兴趣查看 FIND 命令的输出,因此您可能应该将输出重定向到 nul

Givendkoa 有一种解决方案,可以工作。

另一种替代方法是使用延迟扩展。

setlocal enableDelayedExpansion
for /f %%d in ('dir /ad /b C:\') do (
  find /c "C:\%%d" exclusions.txt >nul
  if !errorlevel! equ 1 (
    REM Do code here
  )
)

这存在风险,如果 %%d 包含 !,则它会在扩展时破坏该值。这可以通过切换循环内的延迟扩展来解决。

另一种选择是使用 || 运算符,这意味着如果上一个命令失败,则执行以下命令。

for /f %%d in ('dir /ad /b C:\') do (
  find /c "C:\%%d" exclusions.txt >nul || (
    REM Do code here
  )
)

也许最好的选择是通过使用 /V 和 /G: 选项将 DIR 的结果直接通过管道传输到 FINDSTR 来完全消除对错误级别的担忧,以过滤掉排除列表中出现的值。

以下内容应该可以工作,

for /f %%d in ('dir /ad /b C:\ ^| findstr /l /v /g:"exclude.list"') do (
  REM - Do code here
)

但是 - 有一个令人讨厌的 FINDSTR 错误,如果您搜索不同长度的多个文字字符串,则可能会导致它失败。

解决方法是强制 FINDSTR 使用正则表达式,但随后您需要转义可能出现在排除列表中的任何正则表达式元字符。例如,名为 myName.ext 的目录必须转义为 myName\.ext

for /f %%d in ('dir /ad /b C:\ ^| findstr /r /v /g:"exclude.list"') do (
  REM - Do code here
)

需要在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

setlocal enableDelayedExpansion
for /f %%d in ('dir /ad /b C:\') do (
  find /c "C:\%%d" exclusions.txt >nul
  if !errorlevel! equ 1 (
    REM Do code here
  )
)

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.

for /f %%d in ('dir /ad /b C:\') do (
  find /c "C:\%%d" exclusions.txt >nul || (
    REM Do code here
  )
)

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

for /f %%d in ('dir /ad /b C:\ ^| findstr /l /v /g:"exclude.list"') do (
  REM - Do code here
)

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 as myName\.ext.

for /f %%d in ('dir /ad /b C:\ ^| findstr /r /v /g:"exclude.list"') do (
  REM - Do code here
)

Other characters that would need to be escaped within exclusion.list are \, ^, $, [, ]

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