Powershell Get-ChildItem -Filter ‘*.txt’ 也会找到’*.txt~’(太贪婪?)

huangapple go评论63阅读模式
英文:

Powershell Get-ChildItem -Filter '*.txt' also finds '*.txt~' (too greedy?)

问题

I'm on Windows 11 with Powershell version 5.1.22621.963.

I want to list the contents of a directory while filtering on files ending in ".txt". I try the following:

> ls -Filter "*.txt";


    Directory: C:\so_example


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----          3/3/2023  11:25 AM              0 example.txt
-a----          3/3/2023  11:25 AM              0 example.txt~

But that also finds files that end in ".txt~" with a tilde, '~', as the last character. Those are backup files added by my text editor. I can remove them, but it gets pretty tedious if I have a large directory with many backup files.

What's going on here? Why is the tilde being matched? How can I stop it from being matched?

I tried forming a search expression that will match only to the end of "txt", like this:

> ls -Filter "*.txt$"

But this returns no results (in the same directory as above).

英文:

I'm on Windows 11 with Powershell vesrion 5.1.22621.963.

I want to list the contents of a directory while filtering on files ending in ".txt". I try the following:

> ls -Filter "*.txt"


    Directory: C:\so_example


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----          3/3/2023  11:25 AM              0 example.txt
-a----          3/3/2023  11:25 AM              0 example.txt~

But that also finds files that end in ".txt~" with a tilde, '~', as the last character. Those are backup files added by my text editor. I can remove them, but it gets pretty tedious if I have a large directory with many backup files.

What's going on here? Why is the tilde being matched? How can I stop it from being matched?

I tried forming a search expression that will match only to the end of "txt", like this:

> ls -Filter "*.txt$"

But this returns no results (in the same directory as above).

答案1

得分: 1

这是.NET API的一个异常情况:

> 当您在searchPattern中使用星号通配符字符并指定三个字符的文件扩展名,例如"*.txt",此方法还会返回具有以指定扩展名开头的文件。

有一个解决方法,但它包括搜索字符串的限制。如果您不使用开头,而是使用一排问号,您会得到预期的结果:

Get-Child-Item -Filter '??????????????????.txt';

限制:如果?的数量太少,您会得到较少的文件名。

1https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.enumeratefiles?view=net-7.0

英文:

This is an anomaly of the .NET API:

> When you use the asterisk wildcard character in searchPattern and you specify a three-character file extension, for example, "*.txt", this method also returns files with extensions that begin with the specified extension.

There is a workaround, but it includes a limitation of the search string. If you don't use the start but instead a row of questions marks, you get the expected result:

Get-Child-Item -Filter '??????????????????.txt'

Limitation: If the number of ? is too small you get less file names.

1https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.enumeratefiles?view=net-7.0

huangapple
  • 本文由 发表于 2023年3月3日 18:39:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定