代码之家  ›  专栏  ›  技术社区  ›  skinnedKnuckles

PowerShell获取子项-深度属性问题

  •  1
  • skinnedKnuckles  · 技术社区  · 7 年前

    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:\> 
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   LotPings    7 年前

    用法 -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