【问题标题】:Powershell Get-ChildItem -Depth property issuesPowershell Get-ChildItem -Depth 属性问题
【发布时间】:2019-01-22 16:27:24
【问题描述】:

Powershell 5.1 版

我有一个文件的 2 个副本,其中一个比另一个嵌套更深。

C:\temp\test.txt
C:\temp\Logs\test.txt

我想使用 Get-ChildItem 来查找较浅(较不深?)的文件。许多帖子建议将 -Path 定义为“C:\temp\*”或“C:\temp\*\*”。但我想使用 Get-ChildItem cmdlet 的 -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

上述所有命令产生相同的结果,即

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:\> 

【问题讨论】:

  • 我认为您错过了第一个命令块中的 -Recurse 参数。使用 \*\* 表示法时,您可以跳过“浅”文件夹
  • 来自我的测试[on win7ps5.1] -Depth 参数 需要 -Recurse 参数和禁止 -Include-Exclude 参数。

标签: powershell get-childitem


【解决方案1】:

-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

【讨论】:

  • 优秀的代码示例。谢谢。我现在可以看到,当我应该使用 -Filter 时,我正在使用 -Include 属性。我的 Win7 Pwsh 5.1 设置不需要 -Recurse 标志。
【解决方案2】:

当使用-include 作为过滤器时,-depth 似乎被忽略(错误?)。

-include 可用于匹配多个条件。 如果您只有一个条件并且想要限制搜索深度,或者使用单个条件进行多次搜索,最好使用-filter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2014-08-29
    • 1970-01-01
    • 2011-02-22
    • 2021-04-27
    • 1970-01-01
    相关资源
    最近更新 更多