Answers:
我能想到的最简单的方法是使用小型PowerShell脚本。如果您正在运行Windows 7,则应该已经安装了Windows 7,否则请访问Microsoft.com下载并安装它。该链接提供了详细的说明,但为了方便起见,此处包括该操作的要点。
打开PowerShell,然后输入以下内容:
(gci C:\Scripts -r | ? {$_.PSIsContainer -eq $True}) | ? {$_.GetFiles().Count -eq 0} | select FullName
将C:\ Scripts更改为您要搜索的任何内容,如果要检查整个驱动器,甚至可以将其设置为C:\。
它将为您提供这样的输出(请注意,这些是C:\ Scripts下面的空目录。
全名 ------- C:\ Scripts \ Empty C:\ Scripts \ Empty文件夹2 C:\ Scripts \ Empty \ Empty子文件夹 C:\ Scripts \ New Folder \ Empty子文件夹深三个级别
如果您稍微研究一下PowerShell,我相信您将能够弄清楚如何根据需要自动删除空文件夹(尽管我建议您以防万一。)
编辑:正如理查德在评论中提到的,对于真正空目录,请使用:
(gci C:\Scripts -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName
?{$_.GetFileSystemInfos().Count -eq 0}
。
以下是我可以找到的最简单的方法,只需一行代码即可。它列出了当前位置的空目录。如果需要递归,则可以将参数-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
尝试这个
Get-ChildItem C:\Scripts -Recurse -Directory | Where-Object {!$_.GetFileSystemInfos().Count}
计数不为0,根本不存在,表示目录完全为空或包含其他完全为空的文件夹
谢谢,我以此为基础编写脚本。我想删除空文件夹,但尝试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)
#>