Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I am looking to write a utility to batch rename a bunch of files at once using a regular expression. The files that I will be renaming all at once follow a certain naming convention, and I want to alter them to a new naming convention using data that's already in the filenames; but not all my files follow the same convention currently.
 
So I want to be able to write a general use program that lets me input into a textbox during runtime the pattern of the filename, and what tokens I want to extract from the filename to use for renaming.
 
For example - Assume I have one file named as such:
 
[Coalgirls]_Suite_Precure_02_(1280x720_Blu-Ray_FLAC)_[33D74D55].mkv
 
I want to be able to rename this file to:
 
Suite Precure - Ep 02 [Coalgirls][33D74D55].mkv
 
This means I would preferably be able to enter into my program before renaming something akin to
[%group%]_Suite_Precure_%ep%_(...)_[%crc%].mkv
and it would populate the local variables "group", "ep", and "crc" to use in the batch rename.
 
How might I approach this?
Posted 15-Dec-12 14:44pm
Comments
Sergey Alexandrovich Kryukov at 15-Dec-12 21:43pm
   
A couple of comments: 1. Matroska is a great thing. 2. You may not like my view, but I would advise: if you want to make a really good usable renamer, make it command-line based, perhaps console only. I would hardly even consider using anything with GUI for such task (but many would disagree).
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use the Regular expression groups:
\[(?<publisher>.*?)\]_Suite_Precure_(?<ep>\d+)_\((?<group>.*?)\)_\[(?<crc>.*?)\].mkv</crc></group></ep></publisher>
This produces 4 groups, so your example becomes:
Publisher   CoalGirls
Ep          02
Group       12080x720_Blu-Ray_FLAC
CRC         33D74D55
You can then use these in the Regex.Replace to generate the new name:
${Group}_Suite_Precure_${Ep}_(${Publisher})_[${CRC}].mkv
Produces
1280x720_Blu-Ray_FLAC_Suite_Precure_02_(Coalgirls)_[33D74D55].mkv
 
However, that is not very user friendly! But it is not easy to do it in a friendly way - you need to specify which "chunks" are which group - which means specifying the start and end sequences, and what to "throw away"
 
(I had a similar problem when renaming large numbers of files, and ended up writing a GUI interface which helped, but frankly was still a PITA to use.)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Check out http://www.mobzystems.com/tools/RegName.aspx[^].
The source code is on CodePlex.
  Permalink  
Comments
agent154 at 17-Dec-12 23:15pm
   
That tool does exactly what I'm looking for. Thank you very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 200
1 George Jonsson 175
2 Kornfeld Eliyahu Peter 169
3 PIEBALDconsult 110
4 Richard MacCutchan 85
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100