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


8

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

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


您正在运行什么版本的Windows?
Windos

Answers:


13

我能想到的最简单的方法是使用小型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

正在使用Windows Vista
Joshua

可以在以下位置下载适用于Vista的PowerShell:microsoft.com/download/en/details.aspx?
id

7
注意:管道将查找不包含文件但包含其他文件夹的文件夹。要使文件夹完全为空,请将管道的第三个元素更改为?{$_.GetFileSystemInfos().Count -eq 0}
理查德

1
@Joshua,您没有将“ C:\ Scripts”更改为要搜索的位置,因此会出现错误“找不到路径'C:\ Scripts',因为它不存在”。就在该行的开头。
Windos

1
@Joshua,您是否阅读过Richards的评论(本主题的第三条)和我对答案的编辑?那只会给您没有文件,没有子目录的目录(即叶节点)
Windos

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


2

尝试这个

Get-ChildItem C:\Scripts -Recurse -Directory | Where-Object {!$_.GetFileSystemInfos().Count}

计数不为0,根本不存在,表示目录完全为空或包含其他完全为空的文件夹


1

谢谢,我以此为基础编写脚本。我想删除空文件夹,但尝试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)
#>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.