Powershell, Sort a list of files by version number at the end of the filename?
You can use the [Version] type to do the version sorting. This only takes the version into account (so it doesn't care about the beginning of the filename):
dir $_ |
Sort-Object {
[Version] $(if ($_.BaseName -match "(\d+_){3}\d+") {
$matches[0] -replace "_", "."
}
else {
"0.0.0.0"
})
} | select -last 1 -ExpandProperty Name
There may be a better way, but this works:
Get-ChildItem $_ |
Sort-Object {[int]$_.Name.Split("_")[2], [int]$_.Name.Split("_")[3], [int]$_.Name.Split("_")[4], [int]$_.Name.Split("_")[5]} -Descending |
select-object -First 1 -Property:Name