简体   繁体   English

使用正则表达式批量重命名文件

[英]Batch rename files with regex

I have a number of files with the following format: 我有许多具有以下格式的文件:

name _ name<number><number> [TIF<11 numbers>].jpg 名称 _ 名称<数字> <数字> [TIF <11数字>]。jpg

eg john_sam01 [TIF 15355474840].jpg 例如john_sam01 [TIF 15355474840].jpg

And I would like to remove the [TIF 15355474840] from all of these files This includes a leading space before the '[TIF...' and a different combination of 11 numbers each time. 我想从所有这些文件中删除[TIF 15355474840] ,其中包括[[TIF ...]之前的前导空格和每次11个数字的不同组合。

So the previous example would become: josh_sam01.jpg 因此,前面的示例将变为: josh_sam01.jpg


In short, using powershell (or cmd.exe) with regex I would like to turn this filename: 简而言之,将Powershell(或cmd.exe)与regex结合使用,我想打开此文件名:

josh_sam01 [TIF 15355474840].jpg josh_sam01 [TIF 15355474840] .jpg

Into this: 变成这个:

josh_sam01.jpg josh_sam01.jpg

With variables being: 'john' 'sam' two numbers and the numbers after TIF. 变量为:'john''sam'两个数字和TIF之后的数字。

Something like, with added newlines for clarity: 类似的内容,其中添加了换行符以使内容更清楚:

dir ‹parameters to select the set of files› |
   % {
     $newName = $_.Name -replace '\s\[TIF \d+\]',''
     rename-item -newname $newName -literalPath $_.Fullname
   }

Almost certainly adding - whatif to the rename until I was sure I had the file selection and rename correct. 几乎可以肯定的加入- whatif到重命名,直到我确信我有文件的选择和重命名正确的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM