如何使用Powershell查找具有给定名称的所有空目录?

如何使用Powershell查找具有给定名称的所有空目录?,powershell,directory,Powershell,Directory,我想使用Powershell查找文件结构中名为“信息与规范”的所有空目录。我以为这条线断了 dir -Directory -recurse | Where-Object {$_.Name -eq "Information & Specification" -and (Measure-Object).count -eq 0} | Select-Object -Property Parent 开始返回结果。但是,它似乎会查找所有“信息和规范”文件夹,而不管它们是否包含文件-如果我将计数条件

我想使用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的文件夹

我跑

得到

Parent
......
A
B
D
这似乎是正确的。但是当我在B/X文件夹中创建一个文件时,我仍然得到了相同的结果,所以很明显我的空性测试是错误的。我回头看了看,试了试

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.

是否有人可以帮助测试空值(或完全替代的解决方案?

获取计数的最简单方法是将get childitem分配给一个变量。然后对该变量进行计数。例如:

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

这将为您提供
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

从这里你可以任意处理结果

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

截断的输出

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

所有这些dir都是空的。[grin]

相当漂亮-
dir-Directory-recurse | where{-NOT$\u.GetFiles()}|选择全名
。我不知道你为什么会收到这个错误消息。谢谢你的空状态有效,当我插入时,我得到了我想要的(A和C/D,但不是B)。。dir-Directory-recurse | Where Object{$$$.Name-eq“X”-and-NOT$$.GetFiles()}选择Object-ExpandProperty fullname作为您尝试失败的原因:
(度量对象)。count
始终为
0
,因为您没有向
度量对象提供任何输入。错误消息源于这样一个事实,即
Get ChildItem$\u
中的
$\u
-不幸的是-没有扩展到完整路径-使用
Get ChildItem$\u全名
;至于发生这种情况的原因:请看,现在我正在我的live案例中尝试,我必须添加-而不是$\ux.GetDirectories()这对我不起作用-我需要Lieven的条件来标识空,但是-ExpandProperty 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