Powershell版本5.1
我有两个文件副本,一个嵌套的比另一个更深,像这样。
C:\temp\test.txt
C:\temp\Logs\test.txt我想用Get-儿童期来寻找较浅的(不深的?)档案。许多帖子建议将-Path定义为"C:\temp\“或"C:\temp\\*”。但是我想使用的-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:\> 发布于 2019-01-22 17:06:39
-Depth的使用似乎排除了-Include或
甚至-Path参数中的通配符。
让-Filter做这个工作,在这个示例树中:
> tree /F
C:.
└───temp
│ Test.txt
│
└───0
│ Test.txt
│
└───1
│ Test.txt
│
└───2
Test.txt这一条衬里:
0..4|%{"-Depth $_ ---------------";(Get-ChildItem -Path C:\Temp\ -Depth $_ -Filter Tes*.txt).FullName}返回:
-Depth 0 ---------------
C:\Temp\Test.txt
-Depth 1 ---------------
C:\Temp\Test.txt
C:\Temp\0\Test.txt
-Depth 2 ---------------
C:\Temp\Test.txt
C:\Temp\0\Test.txt
C:\Temp\0\1\Test.txt
-Depth 3 ---------------
C:\Temp\Test.txt
C:\Temp\0\Test.txt
C:\Temp\0\1\Test.txt
C:\Temp\0\1\2\Test.txt
-Depth 4 ---------------
C:\Temp\Test.txt
C:\Temp\0\Test.txt
C:\Temp\0\1\Test.txt
C:\Temp\0\1\2\Test.txt发布于 2020-09-24 08:50:00
当使用-include作为筛选器时,似乎忽略了-depth (bug?)。
-include可用于匹配多个标准。如果您只有一个条件,并且希望限制搜索深度,或者使用单个条件进行多次搜索,则最好使用-filter。
https://stackoverflow.com/questions/54312527
复制相似问题