#1 : 09/03-23 03:57 Tim
Posts: 2
|
I have thousands of files that fall in one of the formats below & I need to get them uniform
6_10_20 6_16_20 TUE 06_18_20 06_20_20 TUE FRI 5_6_22 MON 10_5_20 😸 SAT 10_10_20🦄 SUN 10_4_20☀ï¸_ THURS 07_07_22 7_28_20 TUES 8_5_23 WED I need them to end up something like "(Week of the year}_08_22_23 MON.xlsx" My first thought was to remove all letter characters but don't know how to focus on removing all the letters except for just using remove and then typing in each day of the week. Then Removing the emojis that appear in some of the names. Adding the week of the year to the front. Then finding a way to add the days of the week back to all of them. I have browsed the forum and have found some solutions that come close to each step but they refer to javascript and need a slight modification to work for what I need and I'm not familiar with js to make the change needed. Any help would be greatly appreciated, Thank you |
#2 : 09/03-23 14:23 David Lee
Posts: 1125
|
You can use a regular expression in a script to separate the parts of the date (remembering to check that there is a match in each file name before processing it).
I am assuming that the day of the week in some of your filenames should correspond to the numeric date. There are some discrepancies amongst your examples - if we can assume that these are typos then we can extract all the day names from the dates via the Date object. Note that JavaScript enumerates months starting from January = zero, so we need to subtract one from the month number when creating the date object (Days of the month are enumerated starting from one). Create a Script method and define a lookup table of day names in the Pre batch script... const days = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]; Then, in the main script... match = item.name.match(/(\d{1,2})_(\d{1,2})_(\d{1,2})/); if (match) { m = ("0" + match[1]).slice(-2); d = ("0" + match[2]).slice(-2); y = match[3]; date = new Date( 1*y+2000, 1*m-1, d); return "{Week of the year}_" + m + "_" + d + "_" + y +"_" + days[date.getDay()]; } You haven't defined what you mean by {Week of the year}- however if you mean the ISO week number then you can create a function to calculate this from the Date object. see: https://weeknumber.com/how-to/javascript#:~:text =To%20get%20the%20ISO%20week,getWeekYear()%20. Put the function definition in the Pre batch script... const days = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]; //Function to return the ISO week of the date. //source: https://weeknumber.com/how-to/javascript#:~:text =To%20get%20the%20ISO%20week,getWeekYear()%20. // Usage: date.getWeek Date.prototype.getWeek = function() { var date = new Date(this.getTime()); date.setHours(0, 0, 0, 0); // Thursday in current week decides the year. date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7); // January 4 is always in week 1. var week1 = new Date(date.getFullYear(), 0, 4); // Adjust to Thursday in week 1 and count number of weeks from date to week1. return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7); } Then call the function from the main script as date.getWeek()... match = item.name.match(/(\d{1,2})_(\d{1,2})_(\d{1,2})/); if (match) { m = ("0" + match[1]).slice(-2); d = ("0" + match[2]).slice(-2); y = match[3]; date = new Date( 1*y+2000, 1*m-1, d); week = date.getWeek(); return "Week " + week + "_" + m + "_" + d + "_" + y +"_" + days[date.getDay()]; } |
#3 : 10/03-23 14:55 Tim
Posts: 2
|
Reply to #2:
Thank you so much! I got it all working, and ended up adding a couple of other methods to get the final result needed but in the end, I never would have been able to complete this work without the great detail/instruction that you provided. You are the hero I needed but not the one I deserved! |