【问题标题】:how do I find all empty directories with a given name using Powershell?如何使用 Powershell 查找具有给定名称的所有空目录?
【发布时间】:2020-01-19 17:23:00
【问题描述】:

我想使用 Powershell 在名为“信息和规范”的文件结构中查找所有空目录。当这条线我以为我已经破解了它

dir -Directory -recurse | Where-Object {$_.Name -eq "Information & Specification" -and (Measure-Object).count -eq 0} | Select-Object -Property Parent

开始返回结果。但是,看起来它会找到所有“信息和规范”文件夹,无论它们是否有文件 - 如果我将计数条件从 -eq 反转为 -ne,我什么也得不到。

所以我在顶层设置了一个简单的测试层次结构,其中包含文件夹 A、B、C。 A 和 B 都包含一个名为 X 的文件夹。(我将在我的测试中搜索 X 而不是“信息和规范”)。文件夹 C 包含文件夹 D,而 D 包含一个名为 X 的文件夹。

我跑了

dir -Directory -recurse | Where-Object {$_.Name -eq "X" -and (Measure-Object).count -eq 0} | Select-Object -Property Parent

得到了

Parent
......
A
B
D

这似乎是正确的。但是当我在 B/X 文件夹中创建一个文件时,我仍然得到相同的结果,很明显我的空性测试是错误的。我参考了Count items in a folder with PowerShell 并尝试了

dir -Directory -recurse | Where-Object {$_.Name -eq "X" -and (Get-ChildItem $_ | Measure-Object).count -eq 0} | Select-Object -Property Parent

但这会产生我不理解的错误消息

Get-ChildItem : Cannot find path 'C:\temp\search_test\X' because it does not exist.

任何人都可以帮助测试空虚(或完全替代的解决方案?)

【问题讨论】:

  • This 非常漂亮 - dir -Directory -recurse | where { -NOT $_.GetFiles()} | Select Fullname。我不知道您为什么会收到该错误消息。
  • 谢谢! - 你的空状态有效,当我将它插入时,我得到了我正在寻找的东西(A 和 C/D,但不是 B).. dir -Directory -recurse | Where-Object {$_.Name -eq "X" -and -NOT $_.GetFiles()} | Select-Object -ExpandProperty FullName
  • 至于为什么你的尝试没有成功:(Measure-Object).count 总是0,因为你没有向Measure-Object 提供任何输入。错误消息源于Get-ChildItem $_ 中的$_ - 不幸的是 - 没有扩展到完整路径 - 改为使用Get-ChildItem $_.FullName;至于为什么会这样:见stackoverflow.com/a/53400031/45375
  • 现在我正在我的现场案例中尝试它,我必须添加 -and -NOT $_.GetDirectories()

标签: powershell directory


【解决方案1】:

这将为您提供C:\Test 下所有空目录的FullName

Get-ChildItem -Path "C:\Test" -Directory -Recurse | Where-Object -FilterScript {($_.GetFiles().Count -eq 0) -and ($_.GetDirectories().Count -eq 0) -and $_.Name -eq "Information & Specification"} | Select-Object -ExpandProperty FullName

从这里您可以随心所欲地处理结果。

【讨论】:

  • 这对我不起作用 - 我需要 Lieven 的条件来识别空,但是 -ExpandProperty FullName 很有用,所以我加了一点
【解决方案2】:

这使用目录的.GetFileSystemInfos() 方法来获取任何子目录和任何文件。输出是目录全名的列表。如果您想要这些对象,请删除最后的 .FullName。 [咧嘴一笑]

$TopDir = 'D:\Temp'
$DirToFind = 'Sample'

$EmptyDirList = @(
    Get-ChildItem -LiteralPath $TopDir -Directory -Recurse |
        Where-Object {
            #[System.IO.Directory]::GetFileSystemEntries($_.FullName).Count -eq 0
            $_.GetFileSystemInfos().Count -eq 0 -and
            $_.Name -match $DirToFind
            }
        ).FullName

$EmptyDirList

截断输出...

D:\Temp\zzz - Copy\Destination\DEcho\Music\Sample Music
D:\Temp\zzz - Copy\Destination\DEcho\Pictures\Sample Pictures
D:\Temp\zzz - Copy\Destination\DEcho\Videos\Sample Videos

[*...snip...*] 

D:\Temp\zzz - Copy\Users - Copy\Admin\Music\Sample Music
D:\Temp\zzz - Copy\Users - Copy\Admin\Pictures\Sample Pictures
D:\Temp\zzz - Copy\Users - Copy\Admin\Videos\Sample Videos

所有这些目录都是空的。 [咧嘴一笑]

【讨论】:

    【解决方案3】:

    获取计数的最简单方法是将 get-childitem 分配给一个变量,然后 .count 该变量。如:

    $fileCount = get-childitem "C:\temp\search_test\X'
    if ($fileCount.count -gt 0)
    {
    Write-Host "Files found!"
    }
    else
    {
    Write-Host "No files found."
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-12
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多