Last active
June 8, 2018 14:58
-
-
Save yv84/c115fc4d099b79c081f1 to your computer and use it in GitHub Desktop.
remove directories older than
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FilenameFilter; | |
import java.io.IOException; | |
import java.nio.file.FileVisitResult; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.SimpleFileVisitor; | |
import java.nio.file.attribute.BasicFileAttributes; | |
import java.util.Arrays; | |
import java.util.Calendar; | |
// find /myFolderToCleanUp/* -mmin -1 -type d -exec rm -f {} \; | |
// FORFILES /S /D -30 /C "cmd /c IF @isdir == TRUE rd @path" | |
// @echo Clean dir: %~dp0 | |
// FORFILES /C "cmd /c IF @isdir == TRUE rd /s /q @path" | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
File folder = new File("/myFolderToCleanUp"); | |
String[] directories = folder.list(new FilenameFilter() { | |
@Override | |
public boolean accept(File current, String name) { | |
return new File(current, name).isDirectory(); | |
} | |
}); | |
for (String dir : directories) { | |
File directory = new File(folder + "/" +dir); | |
Path directoryPath = directory.toPath(); | |
if (getDirOlderThanMinutes(directoryPath, -1)) { | |
System.out.println("remove -> " + directoryPath); | |
try { | |
removeDirectory(directoryPath); | |
System.out.println("ok"); | |
} catch (IOException e) { | |
System.out.println("error: " + e); | |
} | |
} else { | |
continue; | |
} | |
}; | |
System.out.println(Arrays.toString(directories)); | |
} | |
private static boolean getDirOlderThanMinutes( | |
Path directoryPath, Integer timeMinutes) throws IOException { | |
BasicFileAttributes attr = Files.readAttributes( | |
directoryPath, BasicFileAttributes.class); | |
System.out.println("creationTime: " + attr.creationTime()); | |
Calendar calendarNow = Calendar.getInstance(); | |
calendarNow.add(calendarNow.MINUTE, timeMinutes); | |
java.util.Date dateCreationDirTime = new java.util.Date( | |
attr.creationTime().toMillis() ); | |
Calendar calendarDir = Calendar.getInstance(); | |
calendarDir.setTime(dateCreationDirTime); | |
return calendarNow.compareTo(calendarDir) > -1; | |
} | |
private static void removeDirectory(Path directoryPath) | |
throws IOException { | |
Files.walkFileTree(directoryPath, new SimpleFileVisitor<Path>() { | |
public FileVisitResult visitFile( | |
Path file, BasicFileAttributes attrs) throws IOException { | |
Files.delete(file); | |
return FileVisitResult.CONTINUE; | |
} | |
public FileVisitResult postVisitDirectory( | |
Path dir, IOException exc) throws IOException { | |
Files.delete(dir); | |
return FileVisitResult.CONTINUE; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment