如何使用 Get-ChildItem 返回没有扩展名的文件? [英] How do I use Get-ChildItem to return files that don't have an extension?

查看:55
本文介绍了如何使用 Get-ChildItem 返回没有扩展名的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取没有文件扩展名的文件列表.考虑我的目录的内容是:

文件夹文件 1文件2.mp4

我的目标是只获取 file1.

运行 Get-ChildItem -Exclude *.* -File 什么也没返回.

运行 Get-ChildItem -Exclude *.* 返回 folderfile1.

运行 Get-ChildItem -File 返回 file1file2.mp4.

知道是否有任何方法可以使用 Get-ChildItem 仅返回 file1?

解决方案

PSv3+ 中,但 在 PowerShell Core v6.x 中不起作用,已修复在 v7 中(请参阅此 GitHub 问题):

Get-ChildItem -File -Filter *.

  • -File 将输出限制为仅文件(而不是目录).

  • -Filter *. 只选择那些没有扩展名的文件.

出于性能原因,

-Filter 通常优于 -Include/-Exclude,因为它在源头进行过滤em>,而不是返回所有对象并让 PowerShell 进行过滤.

PSv2 中,-File 开关不可用,您需要额外的 Where-Object 调用来限制结果为文件,正如 TheIncorrigible1 指出的:

Get-ChildItem -Filter *.|Where-Object { -not $_.PSIsContainer }

较慢的 PowerShell Core 解决方案:

Get-ChildItem -File |Where-Object -Not 扩展

<小时>

可选背景信息:

-Filter 参数由底层提供程序处理,而不是由 PowerShell 处理,这意味着它的行为可能与 PowerShell 不同,这里确实是这种情况:文件系统提供程序使用Windows API 的通配符表达式匹配,它的功能比 PowerShell 少,但也有一些历史上的怪癖;此外,它仅限于单个通配符表达式,而-Include/-Exclude 支持多个(用、分隔)代码>).

然而,在这里,-Filter 提供了 PowerShell 的通配符匹配所没有的功能:使用 *. 匹配没有扩展名的文件/目录.

-Include/-Exclude 通常以牺牲性能为代价提供功能优势,但它们有自己的局限性和怪癖::>

  • *. 不支持匹配 PowerShell 的通配符语言,据我所知,没有基于通配符的方法来实现这一点.

  • -Include/-Exclude指定或隐含路径的最后一个组件进行操作,因此如果您隐式地定位当前目录,它们将应用于该目录路径,而不是其中的单个文件.

    • 指定 -Recurse 会改变这一点,但会搜索整个目录 subtree.
    • 虽然您应该能够添加-Depth 0来限制对直接子项的匹配,同时仍然能够应用-Include/-Exclude,这在 Windows PowerShell v5.1 中被破坏:-Depth 参数被忽略这种情况.
      不过,此问题已在 PowerShell Core 中修复.

简而言之:-Include/-Exclude 此处不提供解决方案.

I want to get a list of files that don't have a filename extension. Consider the content of my directory to be:

folder
file1
file2.mp4

My goal would be to get file1 only.

Running Get-ChildItem -Exclude *.* -File returned nothing.

Running Get-ChildItem -Exclude *.* returned folder and file1.

Running Get-ChildItem -File returned file1 and file2.mp4.

Any idea if there is any way of using Get-ChildItem to only return file1?

解决方案

In PSv3+, but doesn't work in PowerShell Core v6.x, fixed in v7 (see this GitHub issue):

Get-ChildItem -File -Filter *.

  • -File limits output to just files (as opposed to directories).

  • -Filter *. selects only those files that have no extension.

-Filter is generally preferable to -Include / -Exclude for performance reasons, because it filters at the source, rather than returning all objects and letting PowerShell do the filtering.

In PSv2, where the -File switch isn't available, you need an additional Where-Object call to limit the results to files, as TheIncorrigible1 points out:

Get-ChildItem -Filter *. | Where-Object { -not $_.PSIsContainer }

Slower PowerShell Core solution:

Get-ChildItem -File | Where-Object -Not Extension


Optional background information:

That a -Filter argument is processed by the underlying provider, not by PowerShell, means that its behavior may differ from PowerShell's, which is indeed the case here: the FileSystem provider uses the Windows API's wildcard-expression matching, which has fewer features than PowerShell's as well as some historical quirks; also, it is limited to a single wildcard expression, whereas -Include / -Exclude support multiple ones (separated with ,).

Here, however, -Filter offers something that PowerShell's wildcard matching doesn't: using *. to match files / directories without extension.

-Include / -Exclude generally offer functional advantages at the expense of performance, but they have their own limitations and quirks:

  • *. isn't supported to match items without extension in PowerShell's wildcard language, and there is no wildcard-based way to achieve this that I know of.

  • -Include / -Exclude operate on the last component of the specified or implied path, so if you're implicitly targeting the current directory, they apply to that directory path, not to the individual files inside.

    • Specifying -Recurse changes that, but that searches the entire directory subtree.
    • While you should be able to add -Depth 0 to limit matches to the immediate child items while still being able to apply -Include / -Exclude, this is broken as of Windows PowerShell v5.1: The -Depth argument is ignored in this case.
      This problem has been fixed in PowerShell Core, however.

In short: -Include / -Exclude offer no solution here.

这篇关于如何使用 Get-ChildItem 返回没有扩展名的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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