// This program recursively walks through a folder and checks all the last modified times of the files contained therein
// and then prints the most recent one (and optionally all files found) and then gives the user the option of deleting the
// folder with all files
import java.util.*;
import java.util.stream.Stream;
import java.nio.file.*;
import java.nio.file.attribute.FileTime;
import java.io.IOException;
import java.io.File;
public class EUIRemover {
ArrayDeque<FileTime> mostRecentTimes = new ArrayDeque<>();
private void processFile(Path p, boolean print) {
try {
FileTime modTime = Files.getLastModifiedTime(p);
if (print) System.out.println(modTime.toString().substring(0, 16) + " " + p.toString());
if (mostRecentTimes.peek() != null && mostRecentTimes.peek().compareTo(modTime) < 0) mostRecentTimes.push(modTime);
else mostRecentTimes.offer(modTime);
} catch (SecurityException e) {
System.err.println("Cannot access this file in the working dir!\n" + p.toString());
} catch (IOException e) {
System.err.println("An IOException occurred while trying to access last-modified time stamps on this file!\n" + p.toString() + "\n" + e.toString());
}
}
private void processFiles(String workingDir, boolean debug) {
try (Stream<Path> pathStream = Files.walk(Paths.get(workingDir))) { // by default Files.walk() does not follow symbolic links
pathStream.filter(e -> Files.isRegularFile(e, LinkOption.NOFOLLOW_LINKS))
.forEach(e -> processFile(e, debug));
} catch (SecurityException e) {
System.err.println("SecurityException caught! Cannot access the provided path " + workingDir + "\n" + e.toString());
System.exit(1);
} catch (IOException e) {
System.err.println("IOException caught!\n" + e.toString());
System.exit(1);
}
if (!mostRecentTimes.isEmpty()) {
System.out.println("\n\nProcessed " + mostRecentTimes.size() + " files, of which the most recently modified was modified on: " + mostRecentTimes.pop().toString());
System.out.println("\n\nDo you wish to (recursively) delete all files and folders inside of and including the targetFolderPath (" + workingDir + ")? (y/n)");
Scanner reader = new Scanner(System.in);
String answer = reader.nextLine();
if (answer.compareToIgnoreCase("y") == 0) {
try (Stream<Path> pathStream = Files.walk(Paths.get(workingDir))){
pathStream.map(Path::toFile)
.sorted(Comparator.reverseOrder())
.forEach(File::delete);
} catch (SecurityException | IOException e) {
System.err.println("Exception caught when attempting to recursively delete the directory! You will need to manually delete the remaining files.\n\n");
e.printStackTrace();
System.exit(1);
}
}
} else System.out.println("We have found no files to check...exiting.");
}
public static void main(String... args) {
String targetFolder = "C:\\Program Files (x86)\\Steam";
if (args.length == 1 && args[0].compareToIgnoreCase("help") == 0) {
System.out.println("\nSyntax: EUIRemover targetFolderPath [optional]debugflag (\"1\")\n\nThis program recursively walks through a folder and checks all the last modified times of the files contained therein and then prints the most recent one (and optionally all files found) and then gives the user the option of deleting the folder with all files. The optional debugflag will cause all files found to be printed along with their last modified times; by default the folder " + targetFolder + " is used, but a different folder can be specified. If \"default\" is entered as the targetFolderPath then the mentioned default folder is used.");
System.exit(0);
} else if (args.length != 0 && args[0].compareToIgnoreCase("default") != 0) targetFolder = args[0];
String debugflag = (args.length > 1) ? args[1] : "0";
System.out.println("\nChecking all files under " + targetFolder + " recursively!");
EUIRemover myRemover = new EUIRemover();
myRemover.processFiles(targetFolder, debugflag.compareTo("1") == 0);
}
}