DonationCoder.com Software > Coding Snack Guidelines
rename files according to a list - not the usual rename - please read on!
TomD101:
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
cranioscopical:
Real men don't need undo. :P
-skwire (December 19, 2010, 11:05 PM)
--- End quote ---
I know, they fly in the face of adversity…
TomD101:
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.
Krishean:
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 ---function exit(a){return WScript.Quit(a);}function echo(a){return WScript.Echo(a);} var fso=WScript.CreateObject('Scripting.FileSystemObject'); exit(main());function main(){ var tmp=[]; // read list if(fso.FileExists('list.txt')){ try{ var list=safe_split(file_get_contents('list.txt')); }catch(err){ // there was an error while reading list.txt echo('Error: Unable to read list.txt!'); return 1; } // parse list var rExp=/^(\d+)\s+([ \w\d]+)$/i; var line=1; for(var i=0;i<list.length;i++){ list[i]=trim(list[i]); // trim off excess whitespace from beginning and end of lines if(list[i]=='')continue; // don't process blank lines if(rExp.test(list[i])){ var obj=rExp.exec(list[i]); tmp.push([obj[1],obj[2]]); }else{ echo('Error: Unable to parse line '+line+' of list.txt'); return 1; } line++; } list=tmp; tmp=[]; // read directory var e=new Enumerator(fso.GetFolder('.').Files); for(;!e.atEnd();e.moveNext()){ var fname=e.item().Name.toLowerCase(); // don't attempt to rename itself, or list.txt if(e.item() == WScript.ScriptFullName || fname == 'list.txt')continue; for(var j=0;j<list.length;j++){ // compare filename if(fname.indexOf(list[j][1].toLowerCase())!=-1){ // create the new filename var newname=list[j][0]+' - '+list[j][1]+'.'+ fso.GetExtensionName(e.item()); tmp.push('"'+e.item().Name+'" => "'+newname+'"'); // rename file e.item().Name=newname; break; } } } }else{ // the script wasn't able to find a list.txt file in the current directory echo('Error: Does list.txt exist in the current directory?'); return 1; } // output a message saying whether files were renamed or not echo(tmp.length>0?'Files that were renamed:\n\t'+ tmp.join('\n\t'):'No files were renamed!'); return 0;} function safe_split(s){return s.replace(/\r\n/g,'\n').replace(/\r/g,'\n').split('\n');}function trim(s){return s.replace(/^\s+/,'').replace(/\s+$/,'');} function file_get_contents(filename){ var ptr=fso.OpenTextFile(filename,1,false); var str=new String(ptr.ReadAll()); ptr.Close(); return str;}
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
TomD101:
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
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version