topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday March 19, 2024, 12:08 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: rename files according to a list - not the usual rename - please read on!  (Read 15054 times)

TomD101

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 48
    • View Profile
    • Donate to Member
Hello Coders,

I am fairly new to downloading mp3-files from a radio station. I usually ONLY download whole albums, not single tracks.

The downloaded files all have the following syntax:
Artist - Title with spaces.mp3

After downloading, they are sorted alphabetically (or in another way the filesystem from windows permits. This is, of course, not the original order on the album any more.

If I now rename the files listed in my directory I could omit the artist and add a number. But the files would be numbered according to the alphabet, not the intended order on the album.

Before you send me to look for any good mp3-tag-editor: Some of the mp3 HAVE complete tags including track number, others do not. So yes, I tried this already.

Example for tracks as saved on my hard disk:
Keith Urban - All for You.mp3
Keith Urban - Georgia Woods.mp3
Keith Urban - Long Hot Summer.mp3
Keith Urban - Put You in a Song.mp3
Keith Urban - Right on Back to You.mp3
Keith Urban - Shut Out the Lights.mp3
Keith Urban - Without You.mp3
Keith Urban - You Gonna Fly.mp3

The Original order on the album was
1   Put You in a Song
2   You Gonna Fly
3   All for You
4   Long Hot Summer
5   Without You
6   Georgia Woods
7   Right on Back to You
8   Shut Out the Lights

For MY needs - and there could be others like me - I would have to rename each file individually by omitting the artist and adding the right track number  (with two digits incl. leading zero) in front of the title before the  " - ".

So the first title in my directory would be:
Keith Urban - All for You.mp3    ---> 03 - All for you.mp3.

So what I want, seems impossible, as the program would have to be able to parse and compare words from a directory with a file list and rename the files due to these variables.

If you tell me this is simply not possible, I can accept that. I did not find a program to achieve this task.

If anything like this already exists, let me know. I learned that most things people want are already done by (donation)coders.

Have a great Christmas season - or any other festivity according your beliefs

Thomas from Berlin, Germany


The more things stay, the more they change the sane.

Krishean

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 75
  • I like pie
    • View Profile
    • Draconis Labs
    • Donate to Member
i could easily do this with a script, the only limitations would be:

1) the formatting of the list.txt file (i'm assuming you would want "<track number> <title with spaces>")
2) the track name in the list.txt file would have to match the track name in the filename exactly (letter case would not matter, and the case used in list.txt would be used when renaming)
Any sufficiently advanced technology is indistinguishable from magic.

- Arthur C. Clarke

TomD101

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 48
    • View Profile
    • Donate to Member
Hello Krishean,

this sounds phantastic. If you have time, create a rough script and I will test it with some samples.
I am not in a hurry, so take your time.

Best wishes

Thomas
The more things stay, the more they change the sane.

timns

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,211
    • View Profile
    • Donate to Member
Directory Opus has rename features which includes sequential numbering and can also parse file pathnames to pull out fields. Couple this with its RegEx capabilities and you may well get somewhere.

There's an excellent forum where you could ask this specific question and probably get a result.

http://www.gpsoft.com.au/
« Last Edit: December 19, 2010, 12:44 PM by timns »

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
All decent MP3 renamers can do that already. They just read the tag info and let you specify how you want the files named.
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

TomD101

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 48
    • View Profile
    • Donate to Member
Hello timns and Renegade,
I used mp3tag to check for track numbers. Almost every album has missing track info - even if it is only one or two - or double entries with the same track number for different titles. So the tag info is of no use for me.

Directory opus appears to have many fascinating options, but I could not find an option, scripting or not, where to assign filenames from a list which is in another order as the files listed in the directory. I even discovered the tools Series Renamer in the Opus-forum, but all elaborate renamers depend on any kind of numbering within the file in the directory. 

I need a tool to read the string from the file name, compare it to the string in the textfile and, if the same, rename the file in the directory with the filename from the textfile.

This is something I haven't found yet - or didn't use the right terms for my search.

Thomas
The more things stay, the more they change the sane.

Krishean

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 75
  • I like pie
    • View Profile
    • Draconis Labs
    • Donate to Member

list.txt:
01 Put You in a Song
02 You Gonna Fly
03 All for You
04 Long Hot Summer
05 Without You
06 Georgia Woods
07 Right on Back to You
08 Shut Out the Lights

rename.js:
Code: Javascript [Select]
  1. function exit(a){return WScript.Quit(a);}
  2. function echo(a){return WScript.Echo(a);}
  3.  
  4. var fso=WScript.CreateObject('Scripting.FileSystemObject');
  5.  
  6. exit(main());
  7. function main(){
  8.         var tmp=[];
  9.         // read list
  10.         if(fso.FileExists('list.txt')){
  11.                 try{
  12.                         var list=safe_split(file_get_contents('list.txt'));
  13.                 }catch(err){ // there was an error while reading list.txt
  14.                         echo('Error: Unable to read list.txt!');
  15.                         return 1;
  16.                 }
  17.                 // parse list
  18.                 var rExp=/^(\d+)\s+([ \w\d]+)$/i;
  19.                 for(var i=0;i<list.length;i++){
  20.                         list[i]=trim(list[i]); // trim off excess whitespace from beginning and end of lines
  21.                         if(list[i]=='')continue; // don't process blank lines
  22.                         if(rExp.test(list[i])){
  23.                                 var obj=rExp.exec(list[i]);
  24.                                 tmp.push([obj[1],obj[2]]);
  25.                         }
  26.                 }
  27.                 list=tmp;
  28.                 tmp=[];
  29.                 // read directory
  30.                 var e=new Enumerator(fso.GetFolder('.').Files);
  31.                 for(;!e.atEnd();e.moveNext()){
  32.                         // don't attempt to rename itself, or list.txt
  33.                         if(e.item() == WScript.ScriptFullName ||
  34.                           e.item().Name == 'list.txt')continue;
  35.                         for(var j=0;j<list.length;j++){
  36.                                 // compare filename
  37.                                 if(e.item().Name.indexOf(list[j][1])!=-1){
  38.                                         // create the new filename
  39.                                         var fname=list[j][0]+' - '+list[j][1]+'.'+
  40.                                           fso.GetExtensionName(e.item());
  41.                                         tmp.push('"'+e.item().Name+'" => "'+fname+'"');
  42.                                         // rename file
  43.                                         e.item().Name=fname;
  44.                                         break;
  45.                                 }
  46.                         }
  47.                 }
  48.         }else{ // the script wasn't able to find a list.txt file in the current directory
  49.                 echo('Error: Does list.txt exist in the current directory?');
  50.                 return 1;
  51.         }
  52.         // output a message saying whether files were renamed or not
  53.         echo(tmp.length>0?'Files that were renamed:\n\t'+
  54.           tmp.join('\n\t'):'No files were renamed!');
  55.         return 0;
  56. }
  57.  
  58. function safe_split(s){return s.replace(/\r\n/g,'\n').replace(/\r/g,'\n').split('\n');}
  59. function trim(s){return s.replace(/^\s+/,'').replace(/\s+$/,'');}
  60.  
  61. function file_get_contents(filename){
  62.         var ptr=fso.OpenTextFile(filename,1,false);
  63.         var str=new String(ptr.ReadAll());
  64.         ptr.Close();
  65.         return str;
  66. }

create the two files in the same directory that you want to rename the files
the format for list.txt is: <number><space><string> just as shown above
run the script and it should work
Any sufficiently advanced technology is indistinguishable from magic.

- Arthur C. Clarke

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
I hope you don't mind, Krish, this is what I came up with for Tom:

Intro:
Tom's Audio Renamer (TAR, for short).   It uses simple regular expressions for matching groups within a filename and allowing the user to use those matched groups to reformat the name.

Download:
http://skwire.dcmemb.../apps/snacks/TAR.zip

Notes/caveats:
  • Pre-populated with a match string and a format string that should work for your files.
  • No settings are currently saved.
  • Minimal testing so use this app with copies of your files, please.

Usage:
  • Drag-n-drop an album's worth of files to the list.
  • Adjust the match and format string if necessary.
  • Highlight a file in the list and move it up and down with ctrl-up and ctrl-down (or the right click menu).
  • Once you have the list in the order you want, click the Rename button.
  • Click the "?" button for more help information.

2010-12-19_193803.pngrename files according to a list - not the usual rename - please read on!
« Last Edit: December 19, 2010, 07:53 PM by skwire »

cranioscopical

  • Friend of the Site
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,776
    • View Profile
    • Donate to Member
Nice!

Feel up to enhancements? If so, how about an undo?

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Real men don't need undo.   :P

TomD101

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 48
    • View Profile
    • Donate to Member
Hello Krishean and Skwire,

you both are just great. Due to time zone differences I just arrived at work and have to wait 8 hours until I can test your solutions.

I'll be back.

Thomas
The more things stay, the more they change the sane.

cranioscopical

  • Friend of the Site
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,776
    • View Profile
    • Donate to Member
Real men don't need undo.   :P
I know, they fly in the face of adversity…

TomD101

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 48
    • View Profile
    • Donate to Member
Hello Skwire,

slick app with a nice GUI. Makes it very easy to organize my files in the right order and then renames it. Especially helpful: the automatic adding of a leading zero.
Great

Hello Krishean,
phantastic. Just edit a textfile to fit my need and it finds the corresponding mp3 even if they are in another order. Just what I wanted.
But I am afraid there are some bugs left.
I tried three directories, checked every list.txt and found no spelling error. Nonetheless, the app omits files. In the first test the app ignored 3 of 12 files.
Next test 3 of 13 files
Last test 2 of 10 files.
They are out of order an I can see no rule in it.
Any idea. I could not find counters in the script, counters with a certain range of lines, that is.
And yes, is there any chance to add a leading zero to two-digit-numbers?

Thanks again for these two little helpers.

Thomas
0:45 a.m. on a Tuesday.


The more things stay, the more they change the sane.

Krishean

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 75
  • I like pie
    • View Profile
    • Draconis Labs
    • Donate to Member
you can add as many digits to the <number> as you want
the only ways i think it would miss a file is if there was an error parsing the line from list.txt, or if it didn't match the text in the filename exactly, however theres a quick fix for that:

Code: Javascript [Select]
  1. function exit(a){return WScript.Quit(a);}
  2. function echo(a){return WScript.Echo(a);}
  3.  
  4. var fso=WScript.CreateObject('Scripting.FileSystemObject');
  5.  
  6. exit(main());
  7. function main(){
  8.         var tmp=[];
  9.         // read list
  10.         if(fso.FileExists('list.txt')){
  11.                 try{
  12.                         var list=safe_split(file_get_contents('list.txt'));
  13.                 }catch(err){ // there was an error while reading list.txt
  14.                         echo('Error: Unable to read list.txt!');
  15.                         return 1;
  16.                 }
  17.                 // parse list
  18.                 var rExp=/^(\d+)\s+([ \w\d]+)$/i;
  19.                 var line=1;
  20.                 for(var i=0;i<list.length;i++){
  21.                         list[i]=trim(list[i]); // trim off excess whitespace from beginning and end of lines
  22.                         if(list[i]=='')continue; // don't process blank lines
  23.                         if(rExp.test(list[i])){
  24.                                 var obj=rExp.exec(list[i]);
  25.                                 tmp.push([obj[1],obj[2]]);
  26.                         }else{
  27.                                 echo('Error: Unable to parse line '+line+' of list.txt');
  28.                                 return 1;
  29.                         }
  30.                         line++;
  31.                 }
  32.                 list=tmp;
  33.                 tmp=[];
  34.                 // read directory
  35.                 var e=new Enumerator(fso.GetFolder('.').Files);
  36.                 for(;!e.atEnd();e.moveNext()){
  37.                         var fname=e.item().Name.toLowerCase();
  38.                         // don't attempt to rename itself, or list.txt
  39.                         if(e.item() == WScript.ScriptFullName ||
  40.                           fname == 'list.txt')continue;
  41.                         for(var j=0;j<list.length;j++){
  42.                                 // compare filename
  43.                                 if(fname.indexOf(list[j][1].toLowerCase())!=-1){
  44.                                         // create the new filename
  45.                                         var newname=list[j][0]+' - '+list[j][1]+'.'+
  46.                                           fso.GetExtensionName(e.item());
  47.                                         tmp.push('"'+e.item().Name+'" => "'+newname+'"');
  48.                                         // rename file
  49.                                         e.item().Name=newname;
  50.                                         break;
  51.                                 }
  52.                         }
  53.                 }
  54.         }else{ // the script wasn't able to find a list.txt file in the current directory
  55.                 echo('Error: Does list.txt exist in the current directory?');
  56.                 return 1;
  57.         }
  58.         // output a message saying whether files were renamed or not
  59.         echo(tmp.length>0?'Files that were renamed:\n\t'+
  60.           tmp.join('\n\t'):'No files were renamed!');
  61.         return 0;
  62. }
  63.  
  64. function safe_split(s){return s.replace(/\r\n/g,'\n').replace(/\r/g,'\n').split('\n');}
  65. function trim(s){return s.replace(/^\s+/,'').replace(/\s+$/,'');}
  66.  
  67. function file_get_contents(filename){
  68.         var ptr=fso.OpenTextFile(filename,1,false);
  69.         var str=new String(ptr.ReadAll());
  70.         ptr.Close();
  71.         return str;
  72. }

changing line 43 as such makes it case-insensitive, so it isn't as picky when matching
i also added in notices for when it can't parse a line in list.txt
Any sufficiently advanced technology is indistinguishable from magic.

- Arthur C. Clarke

TomD101

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 48
    • View Profile
    • Donate to Member
edited two hours later.
Hello Krishean,
this time I didn't wait until afternoon.

I copied 11 mp3 to a directory, created the list.txt and used the new rename.js.

I know what keeps the app from renaming all files. Special characters like brackets ( ) or hyphens '  are probably dependent on the country codepage, so the app ignores any files with these special characters. As soon as I delete any special character, it works like a charm.

Differences of capital letters between list.txt and filenames are successfully ignored now.

On a short notice: Is it possible to  have the leading zero added to the renaming process, even if it is not in the list.txt.
Example
Fuel - Hemorrhage (In My Hands).mp3 - file - (had to del the brackets) -> Fuel - Hemorrhage In My Hands.mp3
1 Hemorrhage (In My Hands) - within list.txt - (had to del the brackets) -> 1 Hemorrhage In My Hands

would be 01 - Hemorrhage In My Hands.mp3 - renamed file

Have fun.

Thomas

The more things stay, the more they change the sane.
« Last Edit: December 21, 2010, 03:12 AM by TomD101 »

Krishean

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 75
  • I like pie
    • View Profile
    • Draconis Labs
    • Donate to Member
Code: Javascript [Select]
  1. function exit(a){return WScript.Quit(a);}
  2. function echo(a){return WScript.Echo(a);}
  3.  
  4. var fso=WScript.CreateObject('Scripting.FileSystemObject');
  5.  
  6. exit(main());
  7. function main(){
  8.         var tmp=[];
  9.         // read list
  10.         if(fso.FileExists('list.txt')){
  11.                 try{
  12.                         var list=safe_split(file_get_contents('list.txt'));
  13.                 }catch(err){ // there was an error while reading list.txt
  14.                         echo('Error: Unable to read list.txt!');
  15.                         return 1;
  16.                 }
  17.                 // parse list
  18.                 var rExp=/^(\d+)\s+([ \w\d]+)$/i;
  19.                 var line=1;
  20.                 for(var i=0;i<list.length;i++){
  21.                         list[i]=trim(list[i]); // trim off excess whitespace from beginning and end of lines
  22.                         if(list[i]=='')continue; // don't process blank lines
  23.                         if(rExp.test(list[i])){
  24.                                 var obj=rExp.exec(list[i]);
  25.                                 if(String(obj[1]).length<2)obj[1]='0'+obj[1];
  26.                                 tmp.push([obj[1],obj[2]]);
  27.                         }else{
  28.                                 echo('Error: Unable to parse line '+line+' of list.txt');
  29.                                 return 1;
  30.                         }
  31.                         line++;
  32.                 }
  33.                 list=tmp;
  34.                 tmp=[];
  35.                 // read directory
  36.                 var e=new Enumerator(fso.GetFolder('.').Files);
  37.                 for(;!e.atEnd();e.moveNext()){
  38.                         var fname=e.item().Name.toLowerCase();
  39.                         // don't attempt to rename itself, or list.txt
  40.                         if(e.item() == WScript.ScriptFullName ||
  41.                           fname == 'list.txt')continue;
  42.                         for(var j=0;j<list.length;j++){
  43.                                 // compare filename
  44.                                 if(fname.indexOf(list[j][1].toLowerCase())!=-1){
  45.                                         // create the new filename
  46.                                         var newname=list[j][0]+' - '+list[j][1]+'.'+
  47.                                           fso.GetExtensionName(e.item());
  48.                                         tmp.push('"'+e.item().Name+'" => "'+newname+'"');
  49.                                         // rename file
  50.                                         e.item().Name=newname;
  51.                                         break;
  52.                                 }
  53.                         }
  54.                 }
  55.         }else{ // the script wasn't able to find a list.txt file in the current directory
  56.                 echo('Error: Does list.txt exist in the current directory?');
  57.                 return 1;
  58.         }
  59.         // output a message saying whether files were renamed or not
  60.         echo(tmp.length>0?'Files that were renamed:\n\t'+
  61.           tmp.join('\n\t'):'No files were renamed!');
  62.         return 0;
  63. }
  64.  
  65. function safe_split(s){return s.replace(/\r\n/g,'\n').replace(/\r/g,'\n').split('\n');}
  66. function trim(s){return s.replace(/^\s+/,'').replace(/\s+$/,'');}
  67.  
  68. function file_get_contents(filename){
  69.         var ptr=fso.OpenTextFile(filename,1,false);
  70.         var str=new String(ptr.ReadAll());
  71.         ptr.Close();
  72.         return str;
  73. }

line 25 adds one leading zero if there are less than two characters in the number
i can also have it strip characters other than letters/numbers/spaces, but that may have unforseen effects
Any sufficiently advanced technology is indistinguishable from magic.

- Arthur C. Clarke