#1 : 22/03-16 17:22 Matthew Surratt
Posts: 2
|
Hello,
Your help would be greatly appreciated. I am trying to take the last section of my filename after the last - be a fixed width filled with 0's. example: 311-1-3.pdf 311-13-1.pdf 311-1-2.pdf 311-1-20.pdf 311-1-20A.pdf to 311-1-003.pdf 311-13-001.pdf 311-1-002.pdf 311-1-020.pdf 311-1-020A.pdf (note: this file has an A on the end. some file end with A-Z. this does not need to be included in the fixed width.) These are real estate map# and I am trying to automate linking these files to online data. I have 33000 instances of this I need corrected. :( Any help is greatly appreciated! Thanks |
#2 : 22/03-16 20:49 Tester123
Posts: 92
|
Try the following Script Method:
---------- Start of Script ---------- var separator = '-'; var padLength = 3; var padCharacter = '0'; // Extract the three sections of the filename divided by the separator var filePart1 = item.name.split(separator)[0]; var filePart2 = item.name.split(separator)[1]; var filePart3 = item.name.split(separator)[2]; // Break down the last section into digits and trailer var filePart3Digits = filePart3.replace(/[^\d]+/g, ''); var filePart3Trailer = filePart3.replace(/[\d]+/g, ''); // Pad the digits component to required length var filePart3DigitsPadded = filePart3Digits; for (var j = 0; j < padLength - filePart3Digits.length; j++) { filePart3DigitsPadded = padCharacter + filePart3DigitsPadded; } // Rebuild the filename var newFileName = filePart1 + separator + filePart2 + separator + filePart3DigitsPadded + filePart3Trailer; return newFileName; ---------- End of Script ---------- Copy the above and paste into the script window. Note: this makes certain assumptions about the structure of your files. E.g. there has to be three sections divided by a hyphen. The last section must be a series of up to 3 digits followed by an optional non digit character. If these are satisfied, then the script should work. There are probably better ways to achieve this, but this idea just popped up and I'm relatively new to JavaScript! |
#3 : 23/03-16 13:42 Matthew Surratt
Posts: 2
|
Reply to #2:
Works Great! Thank you!!!! |