更新时间:2022-11-30 12:40:54
你可以使用这个:
$tdc="C:\a\c\d"
$dirs = gci $tdc -directory -recurse | Where { (gci $_.fullName).count -eq 0 } | select -expandproperty FullName
$dirs | Foreach-Object { Remove-Item $_ }
$dirs
将是过滤后从 Get-ChildItem
命令返回的空目录数组.然后,您可以遍历它以删除项目.
$dirs
will be an array of empty directories returned from the Get-ChildItem
command after filtering. You can then loop over it to remove the items.
如果您想删除包含空目录的目录,您只需要继续运行脚本,直到它们全部消失.您可以循环直到 $dirs
为空:
If you want to remove directories that contain empty directories, you just need to keep running the script until they're all gone. You can loop until $dirs
is empty:
$tdc="C:\a\c\d"
do {
$dirs = gci $tdc -directory -recurse | Where { (gci $_.fullName).count -eq 0 } | select -expandproperty FullName
$dirs | Foreach-Object { Remove-Item $_ }
} while ($dirs.count -gt 0)
如果您想确保隐藏的文件和文件夹也将被删除,请包含 -Force
标志:
If you want to ensure that hidden files and folders will also be removed, include the -Force
flag:
do {
$dirs = gci $tdc -directory -recurse | Where { (gci $_.fullName -Force).count -eq 0 } | select -expandproperty FullName
$dirs | Foreach-Object { Remove-Item $_ }
} while ($dirs.count -gt 0)