Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ### Stuff you can change
- //Possible date separator in original filename. You can put more in there.
- const DATE_SEP = '-_';
- // Possible characters before the date to remove
- const BFR_DATE_SEP = ' _';
- // date separator for the final filename. Can be changed to taste.
- const NEW_DATE_SEP = '-';
- // Separator in the final filename between the rest of the filename and the date
- const NEW_SEP = ' ';
- // Day and month digit size (pad with zero if necessary)
- const MNT_DAY_SIZE = 2;
- // ### From here, mostly Stuff you shouldn't change, unless you know what you're doing of course.
- // positions of the regex resulting array, to clarify things
- const FULL_RSLT = 0, BEF_DATE = 1, DATE_DAY = 2, DATE_MNT = 3, DATE_YR = 4, AFT_DATE = 5;
- //final values for the date components will be stored here.
- var newDay = "", newMonth = "", newYear = "";
- // Regular expression to split the filename around the date.
- var reDateExtract = new RegExp("(.*?)[" + BFR_DATE_SEP + "]?0?(\\d{1,2})[" + DATE_SEP + "]0?(\\d{1,2})[" + DATE_SEP + "](\\d{2})[\\s_]?(.*)", "");
- // If the regex fails (return nothing) display an error and exit the script
- if (reDateExtract.test(item.newBasename) == False) { return null; }
- // Run the regex and return it in an array
- aDate_n_Extra = reDateExtract.exec(item.newBasename);
- // add proper century to the year
- if (aDate_n_Extra[DATE_YR] >= 30 && aDate_n_Extra[DATE_YR] <=99)
- { newYear = '19' + aDate_n_Extra[DATE_YR]; }
- else
- { newYear = '20' + aDate_n_Extra[DATE_YR]; }
- // padding day and month with a zeros
- newDay = zpad(aDate_n_Extra[DATE_DAY], MNT_DAY_SIZE);
- newMonth = zpad(aDate_n_Extra[DATE_MNT], MNT_DAY_SIZE);
- // Final filename
- return aDate_n_Extra[BEF_DATE] + aDate_n_Extra[AFT_DATE] + NEW_SEP + [newYear, newMonth, newDay].join(NEW_DATE_SEP);
- // Function that add leading zeros to any string
- function zpad(str, nb_digits)
- {
- if (str.length < nb_digits)
- { return (Array(nb_digits - str.length + 1).join('0') + str); }
- else
- { return str; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement