如何使用 Powershell 脚本在 Windows 中查找空目录

如何使用 Powershell 脚本在 Windows 中查找空目录

在 Windows 中使用什么命令来查找空目录?

一些文件夹可能包含一些隐藏文件夹,如.svn.settings,但它们仍应被视为空文件夹。

答案1

我能想到的最简单的方法是用一个小的PowerShell 脚本。如果您运行的是 Windows 7,则应该已经安装了它,如果没有,请访问 Microsoft.com 下载并安装它。该链接提供了详细说明,但此处包含了操作的要点,以方便您使用。

打开 PowerShell 并输入:

(gci C:\Scripts -r | ? {$_.PSIsContainer -eq $True}) | ? {$_.GetFiles().Count -eq 0} | select FullName

将 C:\Scripts 更改为您想要搜索的任何内容,如果您希望它检查整个驱动器,您甚至可以将其设置为 C:\。

它将为您提供如下输出(请注意,这些是 C:\Scripts 下面的空目录。

FullName
------- 
C:\Scripts\Empty 
C:\Scripts\Empty Folder 2 
C:\Scripts\Empty\Empty Subfolder 
C:\Scripts\New Folder\Empty Subfolder Three Levels Deep

如果您稍微研究一下 PowerShell,我相信您就能弄清楚如何自动删除空文件夹(尽管我建议不要这样做,以防万一。)

编辑:正如 Richard 在评论中提到的,对于真正空的目录使用。它还会搜索隐藏文件和文件夹:

(gci C:\Scripts -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName

答案2

以下是我能找到的用一行代码实现此目的的最简单方法。它列出了当前位置的空目录。如果需要递归,-Recurse可以将参数添加到对的调用中Get-ChildItem

Get-ChildItem -Directory | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }

带别名的简短版本:

dir -Directory | ? {$_.GetFileSystemInfos().Count -eq 0 }

或者,作为参数化的 PowerShell 函数(我将其添加到我的 PowerShell 启动配置文件中):

Function Get-EmptyDirectories($basedir) { 
    Get-ChildItem -Directory $basedir | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }
}

然后可以像调用任何其他 PowerShell 函数(包括管道)一样调用它。例如,此调用将删除系统临时目录中的所有空目录:

Get-EmptyDirectories $env:TMP | del

答案3

谢谢,我用这个作为我的脚本的基础。我想删除空文件夹,但尝试这样做Where-Object {$_.GetFiles().Count -eq 0}会删除包含非空子目录的文件夹。我最终使用 DO WHILE 循环删除没有文件或文件夹的文件夹,然后循环回来并再次检查,直到到达树的末尾。

$Datefn=Get-Date -format M.d.yyyy_HH.mm.ss
#Set The File Name for the log file
$DelFileName = $Datefn
#Set The File Ext for the log file
$DelFileExt = " - Old Files" + ".log"
#Set The File Name With Ext for the log file
$DelFileName = $DelFileName + $DelFileExt
#Set Log Path
$LogPath = [Environment]::GetFolderPath("Desktop")
$Path = 'Q:\'
$NumDays = 365
Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
(Get-Date).addDays(-$NumDays) -and $_.psiscontainer -eq $false} |
    ForEach-Object  {
        $properties = @{`
            Path = $_.Directory`
            Name = $_.Name
            DateModified = $_.LastWriteTime
            Size = $_.Length / 1GB  }
    New-Object PSObject -Property $properties | select Path,Name,DateModified, Size
    } |
    Out-File "$LogPath\$DelFileName"
<#
#Removes the files found
Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
(Get-Date).addDays(-365) -and $_.psiscontainer -eq $false} | Remove-Item -Recurse -Force
#Removes empty folders
DO {
$a = (Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
{$_.GetFileSystemInfos().Count -eq 0} | Select-Object Fullname
$a
(Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
{$_.GetFileSystemInfos().Count -eq 0} | Remove-Item -Force
}
WHILE ($a -ne $null)
#>

答案4

上述几个答案的组合,但如果有人需要循环遍历空目录

foreach($emptyDir in Get-ChildItem $outputdir -Recurse -Directory | Where-Object {!$_.GetFileSystemInfos().Count} | select FullName)
{
    Write-Host $emptyDir.FullName
}

相关内容