SOLVED

Tri croissant numérique des fichiers d'un répertoire donné et ayant pour nom un nombre*

Copper Contributor

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 je souhaite obtenir une suite numérique croissante des fichiers enregistrés dans un répertoire donné c-à-d allant de 1 à n (1,2,3,4,5...10,11,12...20,21...n) de façon croissante et non pas une suite de type 1 10 11 12 2 20 21

 

Voici la question telle qu'elle à été posée :

 

dans un répertoire j'ai des fichiers :
1.png
2.png
3.png
...
10.png
11.png
12.png
...
21.png
22.png

...

Get-ChildItem C:\dba\DL -Name | Sort-Object name me renvoie :
1.png
10.png
11.png
12.png
...
2.png
20.png
21.png
22.png
23.png

...

Mon but est d'avoir les chiffres qui se suivent et j'ai 130 fichiers

comment faire?

 

En vous remerciant d'avance pour votre aide.

1 Reply
best response confirmed by WCH79 (Copper Contributor)
Solution

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

 

Sort FileNames.png

 

I am sure there are more elegant and concise solutions for this task but this one also works)

Hope that helps.

 

1 best response

Accepted Solutions
best response confirmed by WCH79 (Copper Contributor)
Solution

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

 

Sort FileNames.png

 

I am sure there are more elegant and concise solutions for this task but this one also works)

Hope that helps.

 

View solution in original post