V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
AndyAO
V2EX  ›  问与答

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

  •  1
     
  •   AndyAO · 2021-07-05 07:41:36 +08:00 · 403 次点击
    这是一个创建于 1193 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想要获取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
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1789 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 89ms · UTC 16:27 · PVG 00:27 · LAX 09:27 · JFK 12:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.