如何像 Windows 资源管理器一样按文件名排序?
这就是应用于 powershell 的“ASCIIbetical”顺序与“自然”顺序的著名问题。为了能够像explorer一样在powershell中进行排序,您可以使用这个包装器 通过 StrCmpLogicalW API,它实际上为 Windows 资源管理器执行自然排序。但这将需要一些管道。
然而,这篇文章建议python 中有一个排序的三行实现。人们希望 Get-ChildItem cmdlet 或至少文件系统提供程序可以具有内置的自然排序选项。不幸的是,他们没有。
那么问题来了,在 Powershell 中最简单的实现是什么?简单地说,我的意思是要编写的代码量最少,并且可能不需要第三方/外部脚本/组件。理想情况下,我想要一个简短 Powershell 函数来为我进行排序。
This is the famous problem of "ASCIIbetical" order versus "Natural" order as applied to powershell. To be able to sort in powershell the same way as explorer does, you can use this wrapper over StrCmpLogicalW API, that actually performs the natural sorting for Windows Explorer. This will require some plumbing though.
However, this article suggests that there is a three liner implementation of the sort in python. One would hope that Get-ChildItem cmdlet or at least File System Provider can have built-in natural sorting option. Unfortunately, they do not.
So here is the question, what is simplest implementation of this in Powershell? By simple I mean the least amount of code to write, and possibly no third-party/external scripts/components. Ideally I want a short Powershell function that would do the sorting for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我更喜欢 @Larry Song 的答案,因为它的排序方式与 Windows 资源管理器完全一样。我尝试稍微简化一下,以减少干扰。
然后你可以像这样使用它:
输出:
I prefer @Larry Song's answer because it sorts exactly the way Windows Explorer does. I tried to simplify it a little to make it less intrusive.
Then you can use it like:
which outputs:
从 python 到 PowerShell 的翻译效果非常好:
使用 代理函数方法。您将
-natur
参数添加到Sort-Object
中,您就得到了非常漂亮的解决方案。更新:首先,我对 PowerShell 以这种方式处理比较数组感到非常惊讶。在我尝试创建测试文件后
("a0", "a100", "a2") + 1..10 + 100..105 | % { '' | Set-Content $env:TEMP\mytestsodir\$_.txt }
,结果发现不行。所以,我认为没有像这样优雅的解决方案,因为 PowerShell 在幕后是静态的,而 python 是动态的。Translation from python to PowerShell works pretty well:
You can do it even better when you use Proxy function approach. You add
-natur
parameter toSort-Object
and you have pretty beautiful solution.Update: First I was quite surprised that PowerShell handles comparing arrays in this way. After I tried to create test files
("a0", "a100", "a2") + 1..10 + 100..105 | % { '' | Set-Content $env:TEMP\mytestsodir\$_.txt }
, it turned out that it doesn't work. So, I think there is no elegant solution like, because PowerShell is static under the covers, whereas python is dynamic.请允许我复制并粘贴另一个问题的答案。
带有数字的 Powershell 排序对象名称不正确< /a>
Windows 资源管理器使用 shlwapi.dll 中名为
StrCmpLogicalW
的旧版 API,这就是看到不同排序结果的原因。我不想补零,所以写了一个脚本。
https://github.com/LarrysGIT/Powershell-Natural-sort
因为我不是作为 C# 专家,如果拉取请求不整洁,我们会表示赞赏。
找到以下PowerShell脚本,它使用相同的API。
查找下面的测试结果。
Allow me to copy and paste my answer from another question.
Powershell Sort-Object Name with numbers doesn't properly
Windows explorer is using a legacy API from shlwapi.dll which called
StrCmpLogicalW
, that's the reason seeing different sorting results.I don't want to pad zeros, so wrote a script.
https://github.com/LarrysGIT/Powershell-Natural-sort
Since I am not a C# expert, pull requests are appreciated if it's not tidy.
Find following PowerShell script, it uses the same API.
Find test results below.
TL;DR
这里有一些非常短的代码(只是
$ToNatural
脚本块),它使用正则表达式和匹配评估器来完成这个技巧,以便用空格填充数字。然后我们像往常一样用填充数字对输入进行排序,并最终得到自然顺序。输出:
最后我们使用这个单行代码按名称按自然顺序对文件进行排序:
输出:
TL;DR
Here is some very short code (just the
$ToNatural
script block) that does the trick with a regular expression and a match evaluator in order to pad the numbers with spaces. Then we sort the input with padded numbers as usual and actually get natural order as a result.Output:
And finally we use this one-liner to sort files by names in natural order:
Output: