簡體   English   中英

Get-ChildItem 遞歸但排除父文件夾中的文件

[英]Get-ChildItem recursively but exclude files in the parent folder

我需要獲取 $PSScriptRoot 的所有子目錄中的文件列表,但排除 $PSScriptRoot 的父文件夾中的所有文件。

$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Recurse | Where-Object {!$_.PSIsContainer}

您可以先列出父文件夾,然后遞歸每個目錄的文件:

$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Directory | Get-ChildItem -File -Recurse

您可以添加Where-Object子句以過濾掉直接位於 $PSScriptRoot 內的所有文件:

$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Recurse -File | Where-Object { $_.DirectoryName -ne $PSScriptRoot}

從 PowerShell 3.0 版開始,您可以使用 switch -File而不是Where-Object {.$_.PSIsContainer}

您只需要再次調用 Get-ChildItem 作為 pipe

$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Recurse | Where-Object {!$_.PSIsContainer} | Get-ChildItem

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM