如何将目录从 [string x]_[string y].properties 批量重命名为 [string y]_[string x].properties?

问题描述 投票:0回答:1

我为我正在处理的项目手动重命名了大约 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

我寻找其他批量重命名选项来寻求帮助,但我找不到任何有助于解决此特定问题的选项。

windows powershell batch-file batch-rename microsoft-file-explorer
1个回答
0
投票

假设你有一个字符串数组:

$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 了解正则表达式详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.