Reputation: 111
I want to sort somes files in the correct order for selecting them later by using a grid. For this, I use this command :
$choose = Get-ChildItem $environment\Aither\Srv\Ptah\Tables\*.sql -Filter "*patch_5*.sql" -File |
# sorted by a calculated name
Sort-Object { if ($_.BaseName -match '(.+-RC)(\d+)$') { '{0}{1:D3}{2}' -f $matches[1], [int]$Matches[2], $_.Extension }
else { '{0}-RC999{1}' -f $_.BaseName, $_.Extension } } |
Out-GridView -Title "Select your files" -OutputMode Multiple
$choose
I obtain that :
The files are sorted well by placing the RC files after those which do not have them. However, I have a problem, the old files are misplaced and I wanted to know if it was possible to sort them while keeping the RCs placed after.
Example before : patch_5.0.10 ; patch_5.0.11 ; patch_5.0.5 ; ... patch_5.1.0-RC2 ;
Desired example : patch_5.0.5; patch_5.0.10; patch_5.0.11;... patch_5.1.0-RC2 ;
Upvotes: 1
Views: 458
Reputation: 61068
You need to specify a calculated file name to sort on.
Depending on how many revisions you may have, I guess it is also importand to sort these numerically correct.
Lets say you have a set of files that would (without sorting) look like this in the gridview:
Then to sort the way you want, and numerically correct you need to do something like this:
Get-ChildItem 'D:\Test' -Filter "*patch_5*.sql" -File |
# sort by a calculated name
Sort-Object { if ($_.BaseName -match '(.+-RC)(\d+)$') { '{0}{1:D3}{2}' -f $matches[1], [int]$Matches[2], $_.Extension }
else { '{0}-RC999{1}' -f $_.BaseName, $_.Extension } } |
Out-GridView -Title "Select your files" -OutputMode Multiple
Output in GridView:
The above assumes you do not have more than 998 revisions.. If you have more, change {1:D3}
to a higher number of digits like {1:D4}
in the if
block and change the hardcoded RC999
to an equally highest number like RC9999
Upvotes: 2
Reputation: 5232
I'd recommend creating an additional property containing only the information you're after. Something like this:
Get-ChildItem path\*.sql -Filter "*patch_5*" |
Select-Object -Property *;@{Name='Version';Expression={[Version]($_.BaseName -split '_')[1]}} |
Sort-Object -Property Version |
Out-GridView -Title "Select your files" -OutputMode Multiple
This way you should get a proper sorting for the version part of the file name.
Upvotes: 0