首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell Get-ChildItem属性问题

Powershell Get-ChildItem属性问题
EN

Stack Overflow用户
提问于 2019-01-22 16:27:24
回答 2查看 4K关注 0票数 2

Powershell版本5.1

我有两个文件副本,一个嵌套的比另一个更深,像这样。

代码语言:javascript
复制
C:\temp\test.txt
C:\temp\Logs\test.txt

我想用Get-儿童期来寻找较浅的(不深的?)档案。许多帖子建议将-Path定义为"C:\temp\“或"C:\temp\\*”。但是我想使用的-Depth参数,或者找出它失败的原因。它应该限制搜索中递归的深度。我读过它暗示递归,因此不需要与recurse一起使用。到目前为止,我已经尝试了下面的所有命令,但它们都返回了向下显示的相同结果。

代码语言:javascript
复制
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

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

代码语言:javascript
复制
FullName : C:\temp\Logs\test.txt
FullName : C:\temp\test.txt

除了-Depth属性之外,按照许多人的建议,使用“*”可以使我隔离更深的文件,而不是更浅的文件。我是不是遗漏了什么?

代码语言:javascript
复制
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:\> 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-22 17:06:39

-Depth的使用似乎排除了-Include

甚至-Path参数中的通配符。

-Filter做这个工作,在这个示例树中:

代码语言:javascript
复制
> tree /F
C:.
└───temp
    │   Test.txt
    │
    └───0
        │   Test.txt
        │
        └───1
            │   Test.txt
            │
            └───2
                    Test.txt

这一条衬里:

代码语言:javascript
复制
 0..4|%{"-Depth $_ ---------------";(Get-ChildItem -Path C:\Temp\ -Depth $_ -Filter Tes*.txt).FullName}

返回:

代码语言:javascript
复制
-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
票数 4
EN

Stack Overflow用户

发布于 2020-09-24 08:50:00

当使用-include作为筛选器时,似乎忽略了-depth (bug?)。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54312527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档