繁体   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