AndyAO
V2EX  ›  问与答

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

  •  1
     
  •   AndyAO · Jul 5, 2021 · 664 views
    This topic created in 1772 days ago, the information mentioned may be changed or developed.

    想要获取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
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2949 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 15:17 · PVG 23:17 · LAX 08:17 · JFK 11:17
    ♥ Do have faith in what you're doing.