Forum Discussion

WCH79's avatar
WCH79
Copper Contributor
Nov 25, 2021
Solved

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...
  • AndySvints's avatar
    Nov 27, 2021

    Hello WCH79,

    One of the options would be to do something like this:

    1. Create Collection
    2. Foreach file in folder create custom PSObject with 2 properties (FileName, Index)
      1. Filename is self explanatory
      2. Index is derived from filename by stripping ".<file extension>" (".txt" in my case)
    3. 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.