我为我正在处理的项目手动重命名了大约 450 个文件,并意识到 400 个文件如果“y”在顺序中排在第一位会更容易,但到那时我已经无法更改它了。有没有办法可以自动做到这一点?
示例:
-cloth_ball.properties
-cloth_puppet.properties
-plastic_ball.properties
-plastic_puppet.properties
-wooden_ball.properties
-wooden_puppet.properties
需要:
-ball_cloth.properties
-ball_plastic.properties
-ball_wooden.properties
-puppet_cloth.properties
-puppet_plastic.properties
-puppet_wooden.properties
我寻找其他批量重命名选项来寻求帮助,但我找不到任何有助于解决此特定问题的选项。
假设你有一个字符串数组:
$strings = @(
'-cloth_ball.properties'
'-cloth_puppet.properties'
'-plastic_ball.properties'
'-plastic_puppet.properties'
'-wooden_ball.properties'
'-wooden_puppet.properties'
)
您可以首先使用正则表达式模式
(?<=-)([^_]+)_([^\.]+)
和 -replace
运算符,然后使用 Sort-Object
对它们进行排序:
$strings -replace '(?<=-)([^_]+)_([^\.]+)', '$2_$1' | Sort-Object
请参阅 https://regex101.com/r/e7kggt/1 了解正则表达式详细信息。