[英]How to move part of a file name to a different position
I have a set of files in a folder.我在一个文件夹中有一组文件。 I would like to edit all file names by moving part of the file name to a different position.我想通过将部分文件名移动到不同的 position 来编辑所有文件名。 That is a sample of what I have:这是我所拥有的样本:
Par1_MD_0_5_AL_2_ND_4_Dist_0_Pot_Drop_out.txt
Par1_MD_0_5_AL_2_ND_4_Dist_1_Pot_Drop_out.txt
Par1_MD_0_5_AL_2_ND_6_Dist_2_Pot_Drop_out.txt
Par1_MD_0_5_AL_2_ND_8_Dist_3_Pot_Drop_out.txt
that is what I want:这就是我想要的:
Par1_MD_0_5_AL_2_Dist_0_ND_4_Pot_Drop_out.txt
Par1_MD_0_5_AL_2_Dist_1_ND_4_Pot_Drop_out.txt
Par1_MD_0_5_AL_2_Dist_2_ND_6_Pot_Drop_out.txt
Par1_MD_0_5_AL_2_Dist_3_ND_8_Pot_Drop_out.txt
Basically, I want to place "ND_(number)" after "Dist_(number)基本上,我想在“Dist_(number)”之后放置“ND_(number)”
Thank you for your help.谢谢您的帮助。
You may try:你可以试试:
(.*?)(ND_\d_)(Dist_\d_)(.*)
Explanation of the above regex:上述正则表达式的解释:
(.*?)
- Represents first capturing group lazily matching everything before ND
. (.*?)
- 表示第一个捕获组延迟匹配ND
之前的所有内容。(ND_\d_)
- Represents second cpaturing group matching ND_
followed by a digit. (ND_\d_)
- 表示第二个匹配组匹配ND_
后跟一个数字。 You can alter if digits are more than one like \d+
.如果数字多于一个,您可以更改\d+
。(Dist_\d_)
- Represents third capturing group matching Dist_
literally followed by a digit. (Dist_\d_)
- 表示第三个捕获组匹配Dist_
字面后跟一个数字。(.*)
- Represents fourth capturing group matching everything after greedily . (.*)
- 表示在greedily之后匹配所有内容的第四个捕获组。$1$3$2$4
- For the replacement part swap groups $3
and $2
to get your required result. $1$3$2$4
- 对于更换部件交换组$3
和$2
以获得您需要的结果。You can find the demo of the above regex in here.您可以在此处找到上述正则表达式的演示。
Powershell Command for renaming the files: Powershell 文件重命名命令:
PS C:\Path\To\MyDesktop\Where\Files\Are\Present> Get-ChildItem -Path . -Filter *.txt | Foreach-Object {
>> $_ | Rename-Item -NewName ( $_.Name -Replace '(.*?)(ND_\d_)(Dist_\d_)(.*)', '$1$3$2$4' )
>> }
Explanation of the above command:上述命令的解释:
1. Get-ChildItem - The Get-ChildItem cmdlet gets the items in one or more specified locations
2. -Path . - Represents a flag option for checking in present working directory.
3. -Filter *.txt - Represents another flag option which filters all the .txt files in present working directory.
4. Foreach-Object - Iterate over objects which we got from the above command.
4.1. Rename-Item -NewName - Rename each item
4.2. $_.Name -Replace - Replace the name of each item containing regex pattern with the replacement pattern.
5. end of loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.