PowerShell 5.1版
我有两个文件副本,一个比另一个嵌套得更深,像这样。
C:\temp\test.txt
C:\temp\Logs\test.txt
我想用get childitem找到较浅的(较浅的?)。文件。许多帖子建议将-path定义为“c:\temp \*”或“c:\temp \*\*”。但我想使用get-childitem cmdlet的-depth参数,或者找出失败的原因。它应该限制搜索中递归的深度。我已经读到它意味着递归,因此不需要与recurse一起使用。到目前为止,我已经尝试了下面的所有命令,但它们都返回了下面显示的相同结果。
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:\>