[英]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.