使用正则表达式将文件按顺序重命名,保持“连接”的文件名模式。

huangapple go评论64阅读模式
英文:

Rename files sequentially keeping "connected" file names pattern using regular expressions

问题

我正在尝试重新组织包含编辑文件和原始文件的照片库。我已经使用Exif Sorter实现了所需的文件夹结构,即%UserProfile%\Photos\%year%\%month%\%day%

每个%day%文件夹包含具有略有不同名称模式的照片图像文件:

IMG_0001.jpg
ZMGM00002.jpg
ZMGM00003 (Edited).jpg
ZMGM00003.jpg
IMG_0002 (Edited).jpg
IMG_0002.jpg
IMG_0004.jpg

我希望文件按顺序命名,但保留相关的“ (Edited)”后缀:

DSC_0001.jpg
DSC_0002.jpg
DSC_0002 (Edited).jpg
DSC_0003.jpg
DSC_0004 (Edited).jpg
DSC_0004.jpg
DSC_0005.jpg

到目前为止,我已经提出了一个正则表达式来重命名“*.jpg”和“* (Edited).jpg”,保留了它的“后缀”部分,当它存在时(抱歉,我使用RegexRenamer,因为我是初学者):

  • 匹配字符串 ^(\D+)(_)?(\d+)(Edited)?
  • 替换字符串 DCS_$#$4

但是,我得到了所有文件的顺序编号,因此编辑文件的相关性丢失了:

DSC_0001.jpg
DSC_0002.jpg
DSC_0003 (Edited).jpg
DSC_0004.jpg
DSC_0005 (Edited).jpg
DSC_0006.jpg
DSC_0007.jpg

是否有任何办法可以重命名文件并保留它们之间的文件名“连接”模式,即使我获得了DSC_0002 (Edited).jpg & DSC_0002.jpg,而不是DSC_0002 (Edited).jpg & DSC_0003.jpg

由于我有数千个文件夹,重命名应该是递归的,每个新文件夹都应重新开始。我认为这需要PowerShell或批处理脚本来确定所需的条件,但我不确定从哪里开始。我愿意尝试一些方法,比如也许我可以首先通过Excel处理文件名,然后从TXT/CSV文件中批量重命名。

附言:我有大约80000张自90年代末以来的家庭照片,手动处理将需要很长时间。我可以在Windows和macOS上运行任何东西来解决这个问题(但更喜欢在Windows上操作)。

英文:

I'm trying to reorganize photo library that contains edited files as well as originals. I already achieved desired folder structure using Exif Sorter, i.e %UserProfile%\Photos\%year%\%month%\%day%.

Each %day% folder contains photo image files with a little bit different name pattern:

IMG_0001.jpg
ZMGM00002.jpg
ZMGM00003 (Edited).jpg
ZMGM00003.jpg
IMG_0002 (Edited).jpg
IMG_0002.jpg
IMG_0004.jpg

I'd like files to be named sequentially but keeping relevant " (Edited)" suffix:

DSC_0001.jpg
DSC_0002.jpg
DSC_0002 (Edited).jpg
DSC_0003.jpg
DSC_0004 (Edited).jpg
DSC_0004.jpg
DSC_0005.jpg

So far I came up with regular expression to rename "*.jpg" and "* (Edited).jpg" preserving it's "suffix" part when it's there (" (Edited)") (sorry I use RegexRenamer because I'm beginner):

  • match string ^(\D+)(_)?(\d+)(Edited)?
  • replace string DCS_$#$4

However I get sequential numbering across all files and thus the relevance of edited files is lost:

DSC_0001.jpg
DSC_0002.jpg
DSC_0003 (Edited).jpg
DSC_0004.jpg
DSC_0005 (Edited).jpg
DSC_0006.jpg
DSC_0007.jpg

Is there any way I can rename files and preserve filename "connection" pattern between them, i.e. so I get DSC_0002 (Edited).jpg & DSC_0002.jpg instead of DSC_0002 (Edited).jpg & DSC_0003.jpg?

Since I've got thousands of folders, the renaming should be recourse & sequence should restarted with each new folder. I believe this requires PowerShell or batch scripting that will determine required condition but I'm not sure where to start. I am open to ideas like maybe I could process file names via Excel first and then batch-rename from TXT/CSV file.

P.S. I've got like 80000 family photos since late 90's, it would take ages to process by hand. I can run anything in Windows and macOS to solve this (would prefer Windows though).

答案1

得分: 0

I'd like to answer my own question because I managed to resolve the problem with the help of the awesome Stefan Trost and his FileRenamer application. Basically Stefan has implemented the feature from scratch and it is available to everyone for free.

Here're FileRenamer settings in case anyone struggles with the same problem in the future:

Settings
Numbering
Reset numbering with every new folder On
Do not increase at files with the same name in the file list one after the other On
Do not increase at files with the same name root in the file list one after the other On

And here're regular expression and it's settings in the main window:

Name
Search for: ^.*([0-9]{2})
Replace with: IMG_%000num%
Interpret as Regular Expression: On

This will go through all sub-folders or folders and rename every file according to the pattern and keeping unique suffixes, just as described in the opening post:

DSC_0001.jpg
DSC_0002.jpg
DSC_0002 (Edited).jpg
DSC_0003.jpg
DSC_0004 (Edited).jpg
DSC_0004.jpg
DSC_0005.jpg

P.S. Here's a link to my request at FileRenamer support portal so nobody thinks this post is an advertisement.

英文:

I'd like to answer my own question because I managed to resolve the problem with the help of the awesome Stefan Trost and his FileRenamer application. Basically Stefan has implemented the feature from scratch and it is available to everyone for free.

Here're FileRenamer settings in case anyone struggles with the same problem in the future:

Settings
Numbering
Reset numbering with every new folder													On
Do not increase at files with the same name in the file list one after the other		On
Do not increase at files with the same name root in the file list one after the other	On

And here're regular expression and it's settings in main window:

Name
Search for:							^.*([0-9]{2})
Replace with:						IMG_%000num%
Interpret as Regular Expression:	On

This will go through all sub-folders or folders and rename every file according to the pattern and keeping unique suffixes, just as described in opening post:

DSC_0001.jpg
DSC_0002.jpg
DSC_0002 (Edited).jpg
DSC_0003.jpg
DSC_0004 (Edited).jpg
DSC_0004.jpg
DSC_0005.jpg

P.S. Here's a link to my request at FileRenamer support portal so nobody thinks this post is an advertisement.

huangapple
  • 本文由 发表于 2023年2月14日 06:40:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定