使用Get-ChildItem Powershell 命令的-Depth属性

3
我想使用Get-Children命令的-Depth参数来查找与下面显示的两个文件同名的较浅文件。"Original Answer"翻译成"最初的回答"。
C:\temp\test.txt
C:\temp\Logs\test.txt

许多帖子建议将-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属性外,像许多人建议的那样使用"\*"可以使我隔离出更深的文件,但不能隔离出更浅的文件。我有什么遗漏吗?

除了使用-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参数。当使用\*\*符号时,你可以跳过“较浅”的文件夹。 - undefined
根据我的测试(在win7ps5.1上),-Depth参数必须-Recurse参数一起使用,并且禁止使用-Include-Exclude参数。 - undefined
2个回答

5
使用 -Depth 似乎排除了在 -Path 参数中使用 -Include 或通配符的可能性。
在这个示例树中,让 -Filter 来处理:
> tree /F
C:.
└───temp
    │   Test.txt
    │
    └───0Test.txt
        │
        └───1Test.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

很棒的代码示例。谢谢。现在我明白了,我在使用-Include属性时应该使用-Filter。在我的Win7 Pwsh 5.1设置中,不需要使用-Recurse标志。 - undefined

1
当使用-include作为过滤器时,-depth似乎被忽略了(bug?)。 -include可以用来匹配多个条件。 如果您只有一个条件并且想限制搜索深度,或者使用单个条件进行多次搜索,最好使用-filter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接