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
|
* @return set containing list of file paths
|
||||||
*/
|
*/
|
||||||
public Set<String> listFilesUsingJavaIO() {
|
public Set<String> listFilesUsingJavaIO() {
|
||||||
|
@ -25,8 +24,7 @@ public class FileList {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* @return array containing list of File objects
|
||||||
* @return array containing list of file names
|
|
||||||
*/
|
*/
|
||||||
public File[] listFilesUsingFileFilter() {
|
public File[] listFilesUsingFileFilter() {
|
||||||
//Creating a File object for directory
|
//Creating a File object for directory
|
||||||
|
@ -40,4 +38,20 @@ public class FileList {
|
||||||
|
|
||||||
return dir.listFiles(fileFilter);
|
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);
|
File directory = new File(dirWay);
|
||||||
if (directory.exists()) {
|
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);
|
long purgeTime = System.currentTimeMillis() - (daysBack * 24 * 60 * 60 * 1000);
|
||||||
for (File listFile : listFiles) {
|
for (File listFile : listFiles) {
|
||||||
if (listFile.lastModified() < purgeTime) {
|
if (listFile.lastModified() < purgeTime) {
|
||||||
if (!listFile.delete()) {
|
if (!listFile.delete()) {
|
||||||
System.err.println("Unable to delete file: " + listFile);
|
System.err.println("Unable to delete file: " + listFile.getName());
|
||||||
Main.LOG.error("file could NOT be deleted: {}", listFiles);
|
Main.LOG.error("file could NOT be deleted: {}", listFile.getName());
|
||||||
} else {
|
} else {
|
||||||
|
LOG.debug("file deleted: {}", listFile.getName());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Main.LOG.debug("file is too young to be deleted");
|
Main.LOG.debug("file is too young to be deleted: {}", listFile.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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();
|
File[] list = fileList.listFilesUsingFileFilter();
|
||||||
LOG.debug("List of files in the specified directory:");
|
LOG.debug("List of files in the specified directory:");
|
||||||
if (list != null) {
|
if (list != null) {
|
||||||
for (File fileName : list) {
|
for (File file : list) {
|
||||||
LOG.debug("{}", fileName);
|
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 {
|
} else {
|
||||||
LOG.warn("List of files is empty");
|
LOG.warn("List of files is empty");
|
||||||
|
|
Loading…
Reference in New Issue