【问题标题】:How to find folders that containt a certain string using CMD?如何使用 CMD 查找包含特定字符串的文件夹?
【发布时间】:2019-10-13 00:36:58
【问题描述】:

我需要在特定位置找到包含“test”或“Test”或“TEST”字样的每个文件夹的路径。文件树很大。

这是删除所有包含“test”的文件夹。我试过使用 findstr 函数。

findstr /i "test"

我希望得到每个包含“test”的文件夹路径

【问题讨论】:

  • 好吧,您告诉findstr,要搜索for 的内容,而不是要搜索in 的内容。试试dir /s /b c:\* | findstr /i "test"
  • 使用dir /s /b /a:d "*test*" 将找到目录。但是,您仍然需要删除它们。

标签: file cmd directory findstr


【解决方案1】:

可以使用FOR 循环生成和迭代目录列表。如果您对删除正确的目录感到满意,请从包含rmdir 的行中删除echo。

@echo off
pushd \
for /f "delims=" %%a in ('dir /s /b /a:d "*test*"') do (
    echo if exist "%%~a" (rmdir /s /q "%%~a")
)
popd

如果您想推进 Powershell(微软称其为未来),这样的事情可能会奏效。如果您对删除正确的目录感到满意,请从 Remove-Item cmdlet 中删除 -WhatIf。

=== Remove-TestDirectories.ps1

$previousdir = ":" # set to impossible filesystem name

Get-ChildItem -Directory -Recurse -Path "C:\" -Filter "*test*" -Force -ErrorAction SilentlyContinue |
    Sort-Object -Property FullName |
    ForEach-Object {
        #"{0} and {1} and {2}" -f @($previousdir, $_.FullName, $_.FullName.StartsWith($previousdir))
        if (-not $_.FullName.StartsWith($previousdir)) {
            $previousdir = $_.FullName
            if (Test-Path -Path $_.FullName) { Remove-Item -Path $_.FullName -Recurse -WhatIf}
        }
    }

这可以从 cmd.exe shell 运行。

powershell -NoLogo -NoProfile -File Remove-TestDirectories.ps1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多