Forum Discussion
WCH79
Nov 25, 2021Copper Contributor
Tri croissant numérique des fichiers d'un répertoire donné et ayant pour nom un nombre*
Bonjour, * Je pense que le nombre en question est un string. Je reprend une question qui n'a pas eu de réponse sur un autre forum. Or comme elle correspond exactement à mon problème à savoir que j...
- Nov 27, 2021
Hello WCH79,
One of the options would be to do something like this:
- Create Collection
- Foreach file in folder create custom PSObject with 2 properties (FileName, Index)
- Filename is self explanatory
- Index is derived from filename by stripping ".<file extension>" (".txt" in my case)
- Sort resulting collection by Index and Select -Expand Filename
#List of PSObjects to Store results $FileCollection=New-Object "System.Collections.Generic.List[PSObject]" #Foreach file in our folder create PSObject with FileName and Index properties Get-ChildItem C:\Code\PS\Local\TempDir -Name | Foreach{ $FileCollection.Add($(New-object -TypeName psobject -Property @{'FileName'=$_;"Index"=$([int]($_.split('.')[0]))}))}; #Sort by Index $FileCollection | Sort-Object -Property Index | Select-Object -ExpandProperty FileNameI am sure there are more elegant and concise solutions for this task but this one also works)
Hope that helps.
AndySvints
Nov 27, 2021Steel Contributor
Hello WCH79,
One of the options would be to do something like this:
- Create Collection
- Foreach file in folder create custom PSObject with 2 properties (FileName, Index)
- Filename is self explanatory
- Index is derived from filename by stripping ".<file extension>" (".txt" in my case)
- Sort resulting collection by Index and Select -Expand Filename
#List of PSObjects to Store results
$FileCollection=New-Object "System.Collections.Generic.List[PSObject]"
#Foreach file in our folder create PSObject with FileName and Index properties
Get-ChildItem C:\Code\PS\Local\TempDir -Name | Foreach{ $FileCollection.Add($(New-object -TypeName psobject -Property @{'FileName'=$_;"Index"=$([int]($_.split('.')[0]))}))};
#Sort by Index
$FileCollection | Sort-Object -Property Index | Select-Object -ExpandProperty FileName
I am sure there are more elegant and concise solutions for this task but this one also works)
Hope that helps.