简体   繁体   English

按文件名末尾的数字对文件进行排序

[英]Sort files by number in the end of a file name

I need help with a tricky task I have.我需要帮助完成一项棘手的任务。

I have a list of *.xml files which have the same ending.我有一个具有相同结尾的 *.xml 文件列表。 The names below show only the naming convention rule.下面的名称仅显示命名约定规则。

EEEEEEEEEE-JJJ-ZZZZZ_DDDDD-XML--0000001629358212.xml EEEEEEEEEE-JJJ-ZZZZZ_DDDDD-XML--0000001629358212.xml

EEEEEEEEEE-JJJ-OOOOOO-XML--0000001533506936.xml EEEEEEEEEE-JJJ-OOOOOO-XML--0000001533506936.xml

EEEEEEEEEE-JJJ-AAAAAA-XML--0000001572627196.xml EEEEEEEEEE-JJJ-AAAAAA-XML--0000001572627196.xml

Filename length maybe is different but it's important for me to sort it by this number in the end.文件名长度可能不同,但对我来说最后按这个数字排序很重要。 With SQL syntax it would be easier but I need a PS solution).使用 SQL 语法会更容易,但我需要一个 PS 解决方案)。

Sort-Object by LastWriteTime works better than other simple ways - but when it comes to a few files with the same hh:mm PS mixes the order. LastWriteTime Sort-Object 比其他简单方法更有效 - 但是当涉及到几个具有相同 hh:mm PS 的文件时,顺序会混合。

At the beginning of the chains of steps that should happen with these files, I remove a time stamps from the beginning of each file name.在这些文件应该发生的步骤链的开头,我从每个文件名的开头删除了时间戳。 I was able to do it with this:我能够做到这一点:

dir *EEEEEEEEEE*.xml | Rename-Item -NewName {$_.name.substring($_.BaseName.IndexOf("EEEEEEEEE"))}

But I'm unable to write something similar for sorting.但我无法为排序编写类似的东西。

Maybe someone can advise how to solve it?也许有人可以建议如何解决它? Maybe You have more experience with PS Substring.也许您对 PS Substring 有更多的经验。

Thanks in advance.提前致谢。

Read and follow Sort-Object , scrolling down to the -Property parameter:阅读并遵循Sort-Object ,向下滚动到-Property参数:


The Property parameter's value can be a calculated property. Property 参数的值可以是计算出的属性。 To create a calculated property, use a hash table.要创建计算属性,请使用哈希表。

Valid keys for a hash table are as follows:哈希表的有效键如下:

 expression - <string> or <script block> ascending or descending - <boolean>

For more information, see about_Calculated_Properties .有关更多信息,请参阅about_Calculated_Properties

Read the Examples section as well.也请阅读示例部分。 Use采用

Get-ChildItem -File -Filter "*EEEEEEEEEE*-*.xml" |
  Sort-Object -Property @{Expression = { ($_.BaseName -split '-')[-1] }}

Thank you guys for the help.谢谢你们的帮助。

I used this line below and it worked in my case.我在下面使用了这一行,它在我的情况下有效。 GCI EEEEEEEEEE*.xml | Sort {$_.Name.substring($_.Name.Length - 20,14)} GCI EEEEEEEEEE*.xml | Sort {$_.Name.substring($_.Name.Length - 20,14)} It reads only the number at the end of the file name and sorts the files in the way I need. GCI EEEEEEEEEE*.xml | Sort {$_.Name.substring($_.Name.Length - 20,14)}它只读取文件名末尾的数字,并按照我需要的方式对文件进行排序。

Best regards最好的祝福

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

相关问题 Powershell,按文件名末尾的版本号对文件列表进行排序? - Powershell, Sort a list of files by version number at the end of the filename? Powershell - 根据文件名将文件分类到目录中 - Powershell - Sort files into directory based on file name 循环前按名称对文件进行排序 - Sort Files By Name Before Looping 根据文件名中的奇数移动文件 - powershell - Move files based on odd number in file name - powershell 根据文件名中间的版本号对文件进行排序 - Sorting of files based on version number in the middle of the file name 将目录中的TXT文件合并为一个文件,并在文件末尾添加一列作为文件名 - Combine TXT files in a directory to one file with column added at end with for file name 如何在多个文件中搜索字符串并在 Excel 或 Powershell 中的 csv 中返回带有行号/文本的文件名 - How to search a string in multiple files and return file name with line number/text in an Excel or csv in Powershell Powershell 多个文件名中的偏移数由一个常数 - Powershell offset number in name of multiple files by a constant Powershell AD脚本-在帐户名末尾添加一个数字 - Powershell AD Script - Add a number onto the end of account Name 使用特定文件名移动文件 - Moving Files with certain File Name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM