在 PowerShell 中如何使用 Exclude 排除特定文件夹的所有内容?

2021-07-05 07:41:36 +08:00
 AndyAO

想要获取tree文件夹中,除了 L1 子文件夹中的所有内容[^1],在当前的情况下,有两个文件,分别是fill.txtdbg.log

本来想用-Exclude,但尝试了很多用法都实现不了:

# 不知道为什么 Exclude 参数好像没有发挥作用
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

为什么我的用法是不对的呢?

[^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
403 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/787513

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX