如何使用-Exclude排除PowerShell中特定文件夹的所有内容?
How to use -Exclude to exclude all the contents of a specific folder in PowerShell?
想要获取 tree
文件夹的所有内容,L1 子文件夹 [^1] 除外。
在当前情况下有两个文件,fill.txt
和 dbg.log
。
本来想用-Exclude
,但是试了很多方法都无法实现。
# I don't know why the exclude parameter doesn't seem to work
Get-ChildItem -Recurse -Path 'tree' -Exclude 'tree\L1' | Should -Not -HaveCount 2
Get-ChildItem -Recurse -Path 'tree' -Exclude 'tree\L1\*' | Should -Not -HaveCount 2
Get-ChildItem -Recurse -Path 'tree' -Exclude "$('tree\L1'|Resolve-Path)*" | Should -Not -HaveCount 2
Where-Object
作品:
Get-ChildItem -Recurse -Path 'tree' | Where-Object {
[string]$_ -notlike "$('tree\L1'|Resolve-Path)*"
} | Should -HaveCount 2
我使用 Exclude
参数的方式有什么问题?如何纠正?
补充:这只是个案。我想知道如何使用 -Exclude
参数来执行此操作。
[^1]:
\tree
├──L1
│ ├──L2
│ │ ├──L3
│ │ │ ├──300.txt
│ │ │ └──xtf.log
│ │ ├──L3_1
│ │ │ ├──123.fp
│ │ │ └──dbg.log
│ │ ├──300.txt
│ │ └──xtf.log
│ ├──L2_1
│ │ ├──150.txt
│ │ └──15x.txt
│ ├──L2_2
│ │ ├──dbg_2.log
│ │ └──dbg.log
│ └──tl.txt
├──dbg.log
└──fill.txt
正如评论中已经提到的,您想要一些使用 -Exclude
参数无法实现的东西。
此参数仅对项目名称起作用。不幸的是,官方文档没有很好地记录这个陷阱。
想要获取 tree
文件夹的所有内容,L1 子文件夹 [^1] 除外。
在当前情况下有两个文件,fill.txt
和 dbg.log
。
本来想用-Exclude
,但是试了很多方法都无法实现。
# I don't know why the exclude parameter doesn't seem to work
Get-ChildItem -Recurse -Path 'tree' -Exclude 'tree\L1' | Should -Not -HaveCount 2
Get-ChildItem -Recurse -Path 'tree' -Exclude 'tree\L1\*' | Should -Not -HaveCount 2
Get-ChildItem -Recurse -Path 'tree' -Exclude "$('tree\L1'|Resolve-Path)*" | Should -Not -HaveCount 2
Where-Object
作品:
Get-ChildItem -Recurse -Path 'tree' | Where-Object {
[string]$_ -notlike "$('tree\L1'|Resolve-Path)*"
} | Should -HaveCount 2
我使用 Exclude
参数的方式有什么问题?如何纠正?
补充:这只是个案。我想知道如何使用 -Exclude
参数来执行此操作。
[^1]:
\tree
├──L1
│ ├──L2
│ │ ├──L3
│ │ │ ├──300.txt
│ │ │ └──xtf.log
│ │ ├──L3_1
│ │ │ ├──123.fp
│ │ │ └──dbg.log
│ │ ├──300.txt
│ │ └──xtf.log
│ ├──L2_1
│ │ ├──150.txt
│ │ └──15x.txt
│ ├──L2_2
│ │ ├──dbg_2.log
│ │ └──dbg.log
│ └──tl.txt
├──dbg.log
└──fill.txt
正如评论中已经提到的,您想要一些使用 -Exclude
参数无法实现的东西。
此参数仅对项目名称起作用。不幸的是,官方文档没有很好地记录这个陷阱。