【发布时间】:2019-02-27 07:03:00
【问题描述】:
Powershell 中的Get-ChildItem -Recurse 当前以级别顺序方式遍历目录。有没有办法在Powershell中以后序方式遍历目录?
我正在尝试删除超过特定时间的文件。删除文件后,如果子文件夹为空,也删除该文件夹。现在正在这样做。
$path = 'D:\Files'
Get-ChildItem -Path $path -Recurse | Where-Object {
(($_.LastWriteTime -lt (Get-Date).AddDays(-30)) -and ($_ -is [system.io.fileinfo]) )
} | Remove-Item
Get-ChildItem -Path $path -Recurse | Where-Object {
($_ -is [System.IO.DirectoryInfo]) -and $_.CreationTime -lt (Get-Date).AddDays(-30) -and ((Get-ChildItem $_.FullName).Count -eq 0)
} | Remove-Item -Force
但我想在一个命令中完成。不像两个不同的命令。
【问题讨论】:
-
请编辑问题并详细解释您要做什么。一个实际的例子,即使是伪代码,也很好。
-
你想达到什么目的才能从其他序列中受益?
-
您真的想以相反的顺序查找项目,还是只是在常规
get-childitem选项之后重新排序? -
问题已更新。
-
顺便说一句:在 PSv3+ 中,您可以分别使用
-File和-Directory将枚举限制为文件和目录。在包含这两种类型的枚举中,$_.PSIsContainer可用于标识目录。
标签: powershell