简体   繁体   English

按get-childitem编号的文件顺序

[英]Order of numbered files by get-childitem

I need to change the order by which get-childitem returns the filenames in a directory because it is important to process them in a specific way. 我需要更改get-childitem返回目录中文件名的顺序,因为以特定方式处理它们很重要。 The filenames have the following format: f_1.csv, f_2.csv, f_3.csv, f_4.csv, ... , f_10.csv, f_11.csv etc The default order that are returned by get-childitem are: f_1.csv, f_10.csv, f_100.csv. 文件名具有以下格式:f_1.csv,f_2.csv,f_3.csv,f_4.csv,...,f_10.csv,f_11.csv等get-childitem返回的默认顺序为:f_1.csv ,f_10.csv,f_100.csv。

One solution would be to change the filenames to f_001.csv, f_002.csv, f_003.csv but I have no control on how the files were created and I do not know their number (may be hundreds, thousands etc) 一种解决方案是将文件名更改为f_001.csv,f_002.csv,f_003.csv,但是我无法控制文件的创建方式,而且我不知道文件的编号(可能是数百,数千等)

My current code is: 我当前的代码是:

    foreach($file in Get-ChildItem $path -Filter f_*.csv) 
    {
        #process the files here
    }

Thank you 谢谢

Something like this? 像这样吗

Get-ChildItem -Filter f_*.csv |
 sort @{Expression={[int]($_.name -replace 'f_(\d+).+','$1')};Descending=$false}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM