4
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

PowerShell - Windowsエクスプローラーと同じ並び順でファイルを取得したい

Get-ChildItem "C:\test"
Mode                LastWriteTime         Length Name             
----                -------------         ------ ----
-a----       2019/05/12     16:08              4 File.txt        
-a----       2019/05/12     16:08              4 File1.txt
-a----       2019/05/12     16:08              4 File11.txt
-a----       2019/05/12     16:08              4 File2.txt
-a----       2019/05/12     16:24              0 File3.txt

こういうとき、エクスプローラーで名前昇順で並び替えたのと同じように、File11が最後に来てほしい。
ググった。あった。
powershell - How to sort by file name the same way Windows Explorer does? - Stack Overflow

Get-ChildItem "C:\test" |
    Sort-Object { [regex]::Replace($_.BaseName, '\d+', { $args[0].Value.PadLeft(20) }) }
Mode                LastWriteTime         Length Name         
----                -------------         ------ ----
-a----       2019/05/12     16:08              4 File.txt
-a----       2019/05/12     16:08              4 File1.txt
-a----       2019/05/12     16:08              4 File2.txt
-a----       2019/05/12     16:24              0 File3.txt
-a----       2019/05/12     16:08              4 File11.txt

でけた。良さそう。
なるほど、正規表現で連続する数字を桁埋めして置き換えた結果で並び替えればいいのね。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
4
Help us understand the problem. What are the problem?