【发布时间】:2019-01-22 16:27:24
【问题描述】:
Powershell 5.1 版
我有一个文件的 2 个副本,其中一个比另一个嵌套更深。
C:\temp\test.txt
C:\temp\Logs\test.txt
我想使用 Get-ChildItem 来查找较浅(较不深?)的文件。许多帖子建议将 -Path 定义为“C:\temp\*”或“C:\temp\*\*”。但我想使用 Get-ChildItem cmdlet 的 -Depth 参数或找出它失败的原因。它应该限制搜索中的递归深度。我读过它意味着递归,因此不需要与递归结合使用。到目前为止,我已经尝试了以下所有命令,但它们都返回了相同的结果。
Get-ChildItem -Path C:\temp -Depth 0 -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 1 -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 2 -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 3 -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth '1' -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth "1" -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth $d -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth $d -Include tes*.txt -Recurse | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 0 -Include tes*.txt -Recurse | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 0 -Include tes*.txt -Recurse | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 2 -Include tes*.txt -Recurse | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 3 -Include tes*.txt -Recurse | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Include tes*.txt -Depth 1 | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Include tes*.txt -Depth 0 | Format-List -Property FullName
Get-ChildItem -Path C:\temp -File -Include tes*.txt -Depth 0 | Format-List -Property FullName
Get-ChildItem -Path C:\temp -File -Include tes*.txt -Depth 1 | Format-List -Property FullName
Get-ChildItem -Path C:\temp -File -Include tes*.txt -Depth 2 | Format-List -Property FullName
Get-ChildItem -Path C:\temp\* -Include tes*.txt -Recurse | Format-List -Property FullName
上述所有命令产生相同的结果,即
FullName : C:\temp\Logs\test.txt
FullName : C:\temp\test.txt
除了 -Depth 属性,使用许多建议的“\*”使我能够隔离较深的文件,但不能隔离较浅的文件。我错过了什么吗?
PS C:\> Get-ChildItem -Path C:\temp\* -Include tes*.txt -Recurse | Format-List -Property FullName
FullName : C:\temp\Logs\test.txt
FullName : C:\temp\test.txt
PS C:\> Get-ChildItem -Path C:\temp\*\* -Include tes*.txt -Recurse | Format-List -Property FullName
FullName : C:\temp\Logs\test.txt
PS C:\> Get-ChildItem -Path C:\temp\*\*\* -Include tes*.txt -Recurse | Format-List -Property FullName
PS C:\>
【问题讨论】:
-
我认为您错过了第一个命令块中的 -Recurse 参数。使用
\*\*表示法时,您可以跳过“浅”文件夹 -
来自我的测试[on win7ps5.1]
-Depth参数 需要-Recurse参数和禁止-Include和-Exclude参数。