// file names need to include the pattern "-YYYYMMDD-HH" static void thinArbitraryDatedBackups(File dir, bool doIt) { new L files; new Map ageMap; Pattern pat = regexp("-(20\\d\\d)(\\d\\d)(\\d\\d)-(\\d\\d)"); for (File f : listFilesNotDirs(dir)) { S s = f.getName(); Matcher matcher = pat.matcher(s); continue unless matcher.find(); //print("Found backup: " + sfu(matcherGroups(matcher))); int year = matcherInt(matcher, 1); int month = matcherInt(matcher, 2); int day = matcherInt(matcher, 3); int hour = matcherInt(matcher, 4); long time = timestampFromYMDH(year, month, day, hour); double age = ((now()-time)/1000.0/60/60/24; //print("Age: " + age + " days"); ageMap.put(f, age); files.add(f); } int numDeleted = 0; sortByMap_inPlace(files, ageMap); double lastAge = -1; for (File f : files) { double age = ageMap.get(f); if (!thinArbitraryDatedBackups_shouldKeep(age, lastAge)) { //print("Deleting: " + f); ++numDeleted; if (doIt) { print("Deleting: " + f); f.delete(); } } else { print("Keeping: " + f); lastAge = age; } } if (numDeleted != 0) print((doIt ? "Deleted: " : "Would delete: ") + n(numDeleted, "file")); } // age = age in days sbool thinArbitraryDatedBackups_shouldKeep(double age, double lastAge) { if (age <= 2) true; if (age <= 31 && age >= lastAge+1) true; if (age >= lastAge+7) true; false; }