Powershell: return filename with highest number

人走茶凉 提交于 2020-01-04 14:08:10

问题


Suppose I have a list of filenames like this:

Get-ChildItem "Antarctica Data *.xls"

Antarctica Data 03625516.xls
Antarctica Data 84327262.xls
Antarctica Data 16374175.xls
Antarctica Data 12804427.xls
Antarctica Data 17477809.xls
Antarctica Data 60943758.xls
Antarctica Data 93962083.xls
Antarctica Data 74412097.xls
Antarctica Data 81562648.xls
Antarctica Data 58371936.xls

How can I use Powershell to return a string of the filename with highest number?

Antarctica Data 93962083.xls

回答1:


Because the names only differ by the numbers, all you need to do is sort them and then get the last one:

Get-ChildItem "Antarctica Data *.xls" |
Sort-Object |
Select-Object -Last 1 -ExpandProperty Name

Or, more concisely:

(gci "Antarctica Data *.xls" | sort)[-1].Name

Output:

Antarctica Data 93962083.xls



来源:https://stackoverflow.com/questions/24916918/powershell-return-filename-with-highest-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!