View Full Version : Script to rename files
03-31-2005, 12:36 AM
I have a problem of renaming a lot of files (mp3's actually) to proper english :)
I already have all my files renamed the way I want to, it's just a matter of what should be uppercase/lowercase. I would like to write a Windows script for this purpose.
All my mp3 files look like this:
'Artist - 01 - Title.mp3' (all uppercase)
Below are the rules that apply to "proper" english and the way I want my files to be:
1. Always capitalize the first and last word of a title/artist.
-i.e. "Children of Bodom - 01 - For the glory" - note the first letter of the title must also be capitalized, even if it's a word like for, of, in,... (see #2)
2. Capitalize all words between the first and last word of a title/artist except:
a, an, the, and, but, or, nor, as, at, by, for, in, of, on, to
3. Do not capitalize words like n' o' de' (i.e. "Rock n' Roll")
-should apply to anywhere in the filename
-i.e. "de' Facto - 01 - Blabla n' Bloblo"
I would really like some help here making a script for this to work properly. I would also like for it to have a function to include subfolders.
If anyone can provide more info on how to do this or maybe has a script like this - major thanks!
03-31-2005, 09:20 PM
This thread might help you out: http://www.codingforums.com/showthread.php?t=49688
04-03-2005, 02:19 PM
There are good tools if you know where to look. Flexible Renamer (http://hp.vector.co.jp/authors/VA014830/english/FlexRena/) is one of the best I've tried, and I've tried a few.
04-03-2005, 05:31 PM
The problem I have is that I can't seem to pass several arguments to the split function. What I'm trying to do is something like:
splitString = fileName.split(" - 01 - " || "- 02 -" || ,...);
So I would end up with to arrays - artist and title. That didn't work, so I tried with something like this:
for(ind = 0; ind < splitString.length; ind++)
if(splitString[ind] == '-')
Now I would like to know why I don't get any ouput of finalString. There are all kinds of functions on slicing up a string/making it an array, but I don't get any output with them - or what am I doing wrong?
I will try that program if it does my job.
04-03-2005, 05:56 PM
String.prototype.split takes a regex as it's argument. That means you can write something like this:
sOrig='Artist -4257 - Song Title',
04-04-2005, 01:54 AM
Thanks liorean, but I took a look at regexp and didn't really understand anything :) Although your code did trim out the '- 01 -' I needed artist and title in 2 seperate strings. I eventually did it with indexOf for artist - looking up the '- 0' and passing it's position to the substring function and lastIndexOf for title - '1 -' again passing it's location to the substring. Very messy with a lot of if statements but I couldn't understand regexp for the hell of me :)
Now that I have artist and title seperated can anyone provide me with some info on how to capitalize first letters of strings with exceptions (for, the, of etc.) and where they appear (if for/of/the is the first or last word it get's capitalized etc).
I did find some scripts that may be worth modifying but they all use arrays and compare chars, but I'm thinking this would be a lot easier with regex (if I would understand them :p) and they don't capitalize a char if it's behind a '(' (i.e. "(for" wouldn't get capitalized.. So if someone knows more about regexp or can give me some hints it would be more than appreciated. The capilazation rules are posted in the first post.
If not I'm thinking of storing each word in a var and compare it.
if(var1 == for || (for || the,...)
04-04-2005, 02:39 AM
Well, you're talking with the right person if you need regex help...
First of all, you were asking for two separate strings. That's exactly what String.prototype.split gave you. It returned an array with two elements in it, the first element being the part of the string before the track number, the second element being the part after the track number. Use hash notation to get the part you want:
sTitle=aNew;Doing the capitalisation is trickier because there are a lot of strange cases, and some aren't that very easy to generalise. Still, I'll try helping you with them.
// I split this up so that you don't have that huge of a single line
// If you prefer a single line, remove aExceptions and rException
// and instead uncomment this line:
// rException=/\b(?:a|an|te|de|the|and|but|or|nor|as|at|by|for|in|o|of|on|to|into|onto|is|am|are|isn|don|doesn|have n|hasn|ain|wasn|weren|aren|t|n|that|such)\b/gi,
// And remove the whitespace in "have n".
// (vBulletin doesn't allow that long unbroken nonwhitespace character sequences)
Note that this will lowercase all "o", "ain", "don" etc.
E.g: It will lowercase "don" in not only "don't" but also "Don Quixote". You might want to fine tune it.
04-04-2005, 04:34 AM
Thanks a lot, that did the trick.
Now there's only one thing left :)
The script shouldn't convert for, of, the etc. if they appear at the start or at the end of the string. I tried the below regex but I can't get it to work (my regex is probably messed up):
rexpOf = /(^of|$of)/g;
rep = new String ("Of");
finalTitle = sFixedTitle.replace(rexpOf, rep);
Oh and another thing. Does the
rFirstWordLetter = /\b[a-z]/gi;
cover such chars as αβă etc... If you don't see correctly these letters - I am talking about the chars with strange umlauts.
edit: Also I would like it to capitalize of, from,... if they appear as the first or last word inside parantheses i.e. "adasd - 01 -asdasd (Of asdlkajd)".
Though I can manage without those if it's too much hassle.