feat(files-age-rm): delete file with age > x
This commit is contained in:
parent
fafd16eb8c
commit
1e92783401
|
@ -14,7 +14,6 @@ public class FileList {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return set containing list of file paths
|
||||
*/
|
||||
public Set<String> listFilesUsingJavaIO() {
|
||||
|
@ -25,8 +24,7 @@ public class FileList {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return array containing list of file names
|
||||
* @return array containing list of File objects
|
||||
*/
|
||||
public File[] listFilesUsingFileFilter() {
|
||||
//Creating a File object for directory
|
||||
|
@ -40,4 +38,20 @@ public class FileList {
|
|||
|
||||
return dir.listFiles(fileFilter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array containing list of File objects
|
||||
*/
|
||||
public static File[] listFiles(String directory) {
|
||||
//Creating a File object for directory
|
||||
File dir = new File(directory);
|
||||
//Creating filter for directory files
|
||||
FileFilter fileFilter = new FileFilter() {
|
||||
public boolean accept(File dir) {
|
||||
return dir.isFile();
|
||||
}
|
||||
};
|
||||
|
||||
return dir.listFiles(fileFilter);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,22 +18,24 @@ public class Main {
|
|||
File directory = new File(dirWay);
|
||||
if (directory.exists()) {
|
||||
|
||||
File[] listFiles = directory.listFiles();
|
||||
//File[] listFiles = directory.listFiles();
|
||||
File[] listFiles = FileList.listFiles(dirWay);
|
||||
LOG.debug("listFiles.length: {}", listFiles.length);
|
||||
long purgeTime = System.currentTimeMillis() - (daysBack * 24 * 60 * 60 * 1000);
|
||||
for (File listFile : listFiles) {
|
||||
if (listFile.lastModified() < purgeTime) {
|
||||
if (!listFile.delete()) {
|
||||
System.err.println("Unable to delete file: " + listFile);
|
||||
Main.LOG.error("file could NOT be deleted: {}", listFiles);
|
||||
System.err.println("Unable to delete file: " + listFile.getName());
|
||||
Main.LOG.error("file could NOT be deleted: {}", listFile.getName());
|
||||
} else {
|
||||
|
||||
LOG.debug("file deleted: {}", listFile.getName());
|
||||
}
|
||||
} else {
|
||||
Main.LOG.debug("file is too young to be deleted");
|
||||
Main.LOG.debug("file is too young to be deleted: {}", listFile.getName());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Main.LOG.warn("directory does not exist");
|
||||
Main.LOG.warn("directory does not exist: {}", directory);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,8 +68,15 @@ public class Main {
|
|||
File[] list = fileList.listFilesUsingFileFilter();
|
||||
LOG.debug("List of files in the specified directory:");
|
||||
if (list != null) {
|
||||
for (File fileName : list) {
|
||||
LOG.debug("{}", fileName);
|
||||
for (File file : list) {
|
||||
LOG.debug("file name: {}", file.getName());
|
||||
LOG.debug("file path: {}", file.getPath());
|
||||
LOG.debug("file abs path: {}", file.getAbsolutePath());
|
||||
try {
|
||||
LOG.debug("file can path: {}", file.getCanonicalPath());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LOG.warn("List of files is empty");
|
||||
|
|
Loading…
Reference in New Issue