简体   繁体   English

Get-ChildItem 递归但排除父文件夹中的文件

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

I need to get a list of files in all subdirectories of $PSScriptRoot but exclude any files in the parent folder of $PSScriptRoot.我需要获取 $PSScriptRoot 的所有子目录中的文件列表,但排除 $PSScriptRoot 的父文件夹中的所有文件。

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

You could list the parent folders first, then recurse each directory for files:您可以先列出父文件夹,然后递归每个目录的文件:

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

You can add a Where-Object clause to filter out all files that are directly inside the $PSScriptRoot:您可以添加Where-Object子句以过滤掉直接位于 $PSScriptRoot 内的所有文件:

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

you can use switch -File instead of Where-Object {.$_.PSIsContainer} as of PowerShell version 3.0从 PowerShell 3.0 版开始,您可以使用 switch -File而不是Where-Object {.$_.PSIsContainer}

You just need to call Get-ChildItem again as a pipe您只需要再次调用 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