使用 Get-ChildItem Powershell cmdlet 的 -Depth 属性

ski*_*les 3 powershell get-childitem

我尝试使用 Get-Children cmdlet 的 -Depth 参数来查找具有相同名称的 2 个文件中较浅(较浅)的文件,如下所示。

C:\temp\test.txt
C:\temp\Logs\test.txt
Run Code Online (Sandbox Code Playgroud)

许多帖子建议将 -Path 定义为“C:\temp\ ”或“C:\temp \\*”。但就我而言,我更喜欢使用 -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
Run Code Online (Sandbox Code Playgroud)

上面的所有命令都会产生相同的结果,即

FullName : C:\temp\Logs\test.txt
FullName : C:\temp\test.txt
Run Code Online (Sandbox Code Playgroud)

除了 -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:\> 
Run Code Online (Sandbox Code Playgroud)

小智 5

的使用似乎排除了参数中通配符的-Depth使用。 -Include
-Path

\n\n

让我们-Filter在这个示例树中完成工作:

\n\n
> tree /F\nC:.\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80temp\n    \xe2\x94\x82   Test.txt\n    \xe2\x94\x82\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x800\n        \xe2\x94\x82   Test.txt\n        \xe2\x94\x82\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x801\n            \xe2\x94\x82   Test.txt\n            \xe2\x94\x82\n            \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x802\n                    Test.txt\n
Run Code Online (Sandbox Code Playgroud)\n\n

这一班轮:

\n\n
 0..4|%{"-Depth $_ ---------------";(Get-ChildItem -Path C:\\Temp\\ -Depth $_ -Filter Tes*.txt).FullName}\n
Run Code Online (Sandbox Code Playgroud)\n\n

返回:

\n\n
-Depth 0 ---------------\nC:\\Temp\\Test.txt\n-Depth 1 ---------------\nC:\\Temp\\Test.txt\nC:\\Temp\\0\\Test.txt\n-Depth 2 ---------------\nC:\\Temp\\Test.txt\nC:\\Temp\\0\\Test.txt\nC:\\Temp\\0\\1\\Test.txt\n-Depth 3 ---------------\nC:\\Temp\\Test.txt\nC:\\Temp\\0\\Test.txt\nC:\\Temp\\0\\1\\Test.txt\nC:\\Temp\\0\\1\\2\\Test.txt\n-Depth 4 ---------------\nC:\\Temp\\Test.txt\nC:\\Temp\\0\\Test.txt\nC:\\Temp\\0\\1\\Test.txt\nC:\\Temp\\0\\1\\2\\Test.txt\n
Run Code Online (Sandbox Code Playgroud)\n