Skip to content

Instantly share code, notes, and snippets.

@pavelgordon
Created March 15, 2018 14:34
Show Gist options
  • Save pavelgordon/0a690c8a079aa866273ef3b9b1892824 to your computer and use it in GitHub Desktop.
Save pavelgordon/0a690c8a079aa866273ef3b9b1892824 to your computer and use it in GitHub Desktop.
Cleaner for file which name contains date older than 90 days. Task from upwork
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Pavel Gordon
*/
public class OldFileCleaner {
public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.out.println("You should specify path to root directory(uploads)");
System.exit(1);
}
String rootUrl = args[0];
LocalDate today = LocalDate.now();
LocalDate thresholdDate = today.minusDays(90);
System.out.println("Delete all files earlier than " + thresholdDate);
Files.walk(Paths.get(rootUrl))
.filter(Files::isRegularFile)
.forEach(file -> {
String fileName = file.getFileName().toString();
System.out.print(file.toAbsolutePath());
String regex = "(\\d{4}-\\d{2}-\\d{2})";
Matcher m = Pattern.compile(regex).matcher(fileName);
if (m.find()) {
LocalDate date = LocalDate.parse(m.group());
if (date.isBefore(thresholdDate)) {
System.out.println(" : delete ");
} else {
System.out.println();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment