【问题标题】:Sort doesnt work in Powershell for list of images [duplicate]排序在Powershell中不适用于图像列表[重复]
【发布时间】:2017-04-27 09:04:41
【问题描述】:

我正在尝试使用以下 powershell 脚本列出文件夹中的图像文件名

Get-ChildItem C:\TestExtraction\ps\Images | Select-Object Name | Sort-Object Name

我有下面的输出

image1.png  
image10.png 
image100.png
image101.png
image102.png
image103.png
image104.png
image105.png
image106.png
image107.png
image108.png
image109.png
image11.png 
image110.png
image111.png
image112.png
image113.png
image114.png
image115.png
image116.png
image117.png
image118.png
image119.png
image12.png 
image120.png
image121.png
image122.png

我在这个简单的脚本中缺少什么,因为没有像

那样正确排序
image1.png
image2.png
image3.png
...

【问题讨论】:

  • 这是混合文本/数字的常见问题。看看stackoverflow.com/questions/1022203/…
  • 我已经试过了..还是不行
  • 使用正则表达式排序。
  • 能否将代码分享给正则表达式方法。
  • 根据@Sean 链接的其他答案,使用这个:Get-ChildItem C:\TestExtraction\ps\Images | Select-Object | Sort-Object { [regex]::Replace($_.Name, '\d+', { $args[0].Value.PadLeft(20) }) }

标签: powershell powershell-3.0


【解决方案1】:

Roman Kuzmin's Answer 在 JamesC 提到的问题中效果很好。

$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
Gci *.jpg | Sort-Object $ToNatural |select Name

样本输出

Name
----
pic1.jpg
pic2.jpg
pic3.jpg
pic4.jpg
pic5.jpg
pic6.jpg
pic7.jpg
pic8.jpg
pic9.jpg
pic10.jpg

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 2020-02-14
    • 2019-11-16
    相关资源
    最近更新 更多