如何从PowerShell中的Get-ChildItem结果中排除项目列表? [英] How to exclude list of items from Get-ChildItem result in powershell?

查看:55
本文介绍了如何从PowerShell中的Get-ChildItem结果中排除项目列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以递归方式获取路径中的文件列表(实际上是文件数),不包括某些类型:

I want to get list of files (actually number of files) in a path, recursively, excluding certain types:

Get-ChildItem -Path $path -Recurse | ? { $_.Name -notlike "*.cs" -and $_.Name -notlike "*.tt" }

但我有一长串排除项(仅举几例):

but I have a long list of exclusions (to name a few):

@("*.cs", "*.tt", "*.xaml", "*.csproj", "*.sln", "*.xml", "*.cmd", "*.txt")

如何使用此表单获取列表:

How to get the list using this form:

Get-ChildItem -Path $path -Recurse | ? { <# what to put here ?#> }

?

推荐答案

这也行:

get-childitem $path -recurse -exclude *.cs,*.tt,*.xaml,*.csproj,*.sln,*.xml,*.cmd,*.txt

请注意,-include 仅适用于路径中的 -recurse 或通配符.(实际上它在 6.1 pre 2 中一直有效)

Note that -include only works with -recurse or a wildcard in the path. (actually it works all the time in 6.1 pre 2)

另请注意,如果路径中没有 -recurse 或通配符,同时使用 -exclude 和 -filter 将不会列出任何内容.

Also note that using both -exclude and -filter will not list anything, without -recurse or a wildcard in the path.

-include 和 -literalpath 在 PS 5 中似乎也有问题.

-include and -literalpath also seem problematic in PS 5.

-include 和 -exclude 也有一个错误,路径位于根\",不显示任何内容.在 unix 中会报错.

There's also a bug with -include and -exclude with the path at the root "\", that displays nothing. In unix it gives an error.

这篇关于如何从PowerShell中的Get-ChildItem结果中排除项目列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆