feat(file-cleaner): refactored class names and dir structure
This commit is contained in:
parent
1e92783401
commit
df1e03d3dd
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,59 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>de.begerad.file-age-rm</groupId>
|
||||||
|
<artifactId>file-age-rm</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-slf4j-impl</artifactId>
|
||||||
|
<version>2.9.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.2.4</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||||
|
<transformers>
|
||||||
|
<transformer implementation=
|
||||||
|
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<mainClass>de.begerad.fileagerm.Main</mainClass>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>8</source>
|
||||||
|
<target>8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,38 @@
|
||||||
|
package de.begerad.fileagerm;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import static de.begerad.fileagerm.Main.LOG;
|
||||||
|
|
||||||
|
public class FileCleaner {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean-up files older than a certain time
|
||||||
|
*
|
||||||
|
* @param age age of files to be cleaned up
|
||||||
|
* @param dirPath directory path of files to be cleaned up
|
||||||
|
*/
|
||||||
|
public static void deleteFilesByAge(int age, String dirPath) {
|
||||||
|
|
||||||
|
File directory = new File(dirPath);
|
||||||
|
if (directory.exists()) {
|
||||||
|
|
||||||
|
File[] listFiles = FileExplorer.listFiles(dirPath);
|
||||||
|
LOG.debug("listFiles.length: {}", listFiles.length);
|
||||||
|
long purgeTime = System.currentTimeMillis() - (age * 24L * 60L * 60L * 1000L);
|
||||||
|
for (File listFile : listFiles) {
|
||||||
|
if (listFile.lastModified() < purgeTime) {
|
||||||
|
if (!listFile.delete()) {
|
||||||
|
LOG.warn("file could NOT be deleted: {}", listFile.getName());
|
||||||
|
} else {
|
||||||
|
LOG.debug("file deleted: {}", listFile.getName());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LOG.debug("file is too young to be deleted: {}", listFile.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LOG.warn("directory does not exist: {}", directory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,10 +6,10 @@ import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class FileList {
|
public class FileExplorer {
|
||||||
private final String directory;
|
private final String directory;
|
||||||
|
|
||||||
public FileList(String directory) {
|
public FileExplorer(String directory) {
|
||||||
this.directory = directory;
|
this.directory = directory;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
package de.begerad.fileagerm;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static de.begerad.fileagerm.FileCleaner.deleteFilesByAge;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static String dirPath = "";
|
||||||
|
|
||||||
|
public final static Logger LOG = LoggerFactory.getLogger(Main.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
LOG.debug("main() started...");
|
||||||
|
|
||||||
|
//check user input for directory
|
||||||
|
if (args.length < 1) {
|
||||||
|
System.out.println("Please enter directory as first parameter.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
dirPath = args[0];
|
||||||
|
if (!(new File(dirPath).isDirectory())) {
|
||||||
|
System.out.println("first parameter: "
|
||||||
|
+ dirPath
|
||||||
|
+ " is NOT a valid directory.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
System.out.println("dir path: "
|
||||||
|
+ (new File(dirPath).getCanonicalPath()));
|
||||||
|
LOG.debug("dir path: {}",
|
||||||
|
(new File(dirPath).getCanonicalPath()));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
FileExplorer fileList = new FileExplorer(dirPath);
|
||||||
|
File[] list = fileList.listFilesUsingFileFilter();
|
||||||
|
LOG.debug("List of files in the specified directory:");
|
||||||
|
if (list != null) {
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> setList = fileList.listFilesUsingJavaIO();
|
||||||
|
LOG.debug("List of files in the specified directory:");
|
||||||
|
for (String file : setList) {
|
||||||
|
LOG.debug("{}", file);
|
||||||
|
}
|
||||||
|
|
||||||
|
int age = 1;
|
||||||
|
long purgeTime = System.currentTimeMillis() - (age * 24L * 60L * 60L * 1000L);
|
||||||
|
LOG.debug("age in ms: {}", purgeTime);
|
||||||
|
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.add(Calendar.DAY_OF_MONTH, age * -1);
|
||||||
|
purgeTime = cal.getTimeInMillis();
|
||||||
|
LOG.debug("age in ms: {}", purgeTime);
|
||||||
|
|
||||||
|
deleteFilesByAge(age, dirPath);
|
||||||
|
|
||||||
|
LOG.debug("main() done.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Configuration status="INFO">
|
||||||
|
<Appenders>
|
||||||
|
<RollingFile name="rollingFile"
|
||||||
|
fileName="log.txt"
|
||||||
|
filePattern="log-%d{yyyy-MM-dd}.txt"
|
||||||
|
ignoreExceptions="false"
|
||||||
|
>
|
||||||
|
<PatternLayout>
|
||||||
|
<Pattern>[%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} %m%n</Pattern>
|
||||||
|
</PatternLayout>
|
||||||
|
<Policies>
|
||||||
|
<SizeBasedTriggeringPolicy size="10MB" />
|
||||||
|
</Policies>
|
||||||
|
<DefaultRolloverStrategy max="5" />
|
||||||
|
</RollingFile>
|
||||||
|
|
||||||
|
<Console name="console" target="SYSTEM_OUT">
|
||||||
|
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} - %msg%n" />
|
||||||
|
</Console>
|
||||||
|
</Appenders>
|
||||||
|
<Loggers>
|
||||||
|
<Logger name="de.begerad" level="debug" additivity="true">
|
||||||
|
<appender-ref ref="rollingFile" level="debug" />
|
||||||
|
</Logger>
|
||||||
|
<Root level="debug" additivity="false">
|
||||||
|
<appender-ref ref="console" />
|
||||||
|
</Root>
|
||||||
|
</Loggers>
|
||||||
|
|
||||||
|
</Configuration>
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,59 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>de.begerad.file-age-rm</groupId>
|
||||||
|
<artifactId>file-age-rm</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-slf4j-impl</artifactId>
|
||||||
|
<version>2.9.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.2.4</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||||
|
<transformers>
|
||||||
|
<transformer implementation=
|
||||||
|
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<mainClass>de.begerad.fileagerm.Main</mainClass>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>8</source>
|
||||||
|
<target>8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,38 @@
|
||||||
|
package de.begerad.fileagerm;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import static de.begerad.fileagerm.Main.LOG;
|
||||||
|
|
||||||
|
public class FileCleaner {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean-up files older than a certain time
|
||||||
|
*
|
||||||
|
* @param age age of files to be cleaned up
|
||||||
|
* @param dirPath directory path of files to be cleaned up
|
||||||
|
*/
|
||||||
|
public static void deleteFilesByAge(int age, String dirPath) {
|
||||||
|
|
||||||
|
File directory = new File(dirPath);
|
||||||
|
if (directory.exists()) {
|
||||||
|
|
||||||
|
File[] listFiles = FileExplorer.listFiles(dirPath);
|
||||||
|
LOG.debug("listFiles.length: {}", listFiles.length);
|
||||||
|
long purgeTime = System.currentTimeMillis() - (age * 24L * 60L * 60L * 1000L);
|
||||||
|
for (File listFile : listFiles) {
|
||||||
|
if (listFile.lastModified() < purgeTime) {
|
||||||
|
if (!listFile.delete()) {
|
||||||
|
LOG.warn("file could NOT be deleted: {}", listFile.getName());
|
||||||
|
} else {
|
||||||
|
LOG.debug("file deleted: {}", listFile.getName());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LOG.debug("file is too young to be deleted: {}", listFile.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LOG.warn("directory does not exist: {}", directory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
package de.begerad.fileagerm;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class FileExplorer {
|
||||||
|
private final String directory;
|
||||||
|
|
||||||
|
public FileExplorer(String directory) {
|
||||||
|
this.directory = directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return set containing list of file paths
|
||||||
|
*/
|
||||||
|
public Set<String> listFilesUsingJavaIO() {
|
||||||
|
return Stream.of(new File(directory).listFiles())
|
||||||
|
.filter(file -> !file.isDirectory())
|
||||||
|
.map(File::getName)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array containing list of File objects
|
||||||
|
*/
|
||||||
|
public File[] listFilesUsingFileFilter() {
|
||||||
|
//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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,80 @@
|
||||||
|
package de.begerad.fileagerm;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static de.begerad.fileagerm.FileCleaner.deleteFilesByAge;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static String dirPath = "";
|
||||||
|
|
||||||
|
public final static Logger LOG = LoggerFactory.getLogger(Main.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
LOG.debug("main() started...");
|
||||||
|
|
||||||
|
//check user input for directory
|
||||||
|
if (args.length < 1) {
|
||||||
|
System.out.println("Please enter directory as first parameter.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
dirPath = args[0];
|
||||||
|
if (!(new File(dirPath).isDirectory())) {
|
||||||
|
System.out.println("first parameter: "
|
||||||
|
+ dirPath
|
||||||
|
+ " is NOT a valid directory.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
System.out.println("dir path: "
|
||||||
|
+ (new File(dirPath).getCanonicalPath()));
|
||||||
|
LOG.debug("dir path: {}",
|
||||||
|
(new File(dirPath).getCanonicalPath()));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
FileExplorer fileList = new FileExplorer(dirPath);
|
||||||
|
File[] list = fileList.listFilesUsingFileFilter();
|
||||||
|
LOG.debug("List of files in the specified directory:");
|
||||||
|
if (list != null) {
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> setList = fileList.listFilesUsingJavaIO();
|
||||||
|
LOG.debug("List of files in the specified directory:");
|
||||||
|
for (String file : setList) {
|
||||||
|
LOG.debug("{}", file);
|
||||||
|
}
|
||||||
|
|
||||||
|
int age = 1;
|
||||||
|
long purgeTime = System.currentTimeMillis() - (age * 24L * 60L * 60L * 1000L);
|
||||||
|
LOG.debug("age in ms: {}", purgeTime);
|
||||||
|
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.add(Calendar.DAY_OF_MONTH, age * -1);
|
||||||
|
purgeTime = cal.getTimeInMillis();
|
||||||
|
LOG.debug("age in ms: {}", purgeTime);
|
||||||
|
|
||||||
|
deleteFilesByAge(age, dirPath);
|
||||||
|
|
||||||
|
LOG.debug("main() done.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Configuration status="INFO">
|
||||||
|
<Appenders>
|
||||||
|
<RollingFile name="rollingFile"
|
||||||
|
fileName="log.txt"
|
||||||
|
filePattern="log-%d{yyyy-MM-dd}.txt"
|
||||||
|
ignoreExceptions="false"
|
||||||
|
>
|
||||||
|
<PatternLayout>
|
||||||
|
<Pattern>[%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} %m%n</Pattern>
|
||||||
|
</PatternLayout>
|
||||||
|
<Policies>
|
||||||
|
<SizeBasedTriggeringPolicy size="10MB" />
|
||||||
|
</Policies>
|
||||||
|
<DefaultRolloverStrategy max="5" />
|
||||||
|
</RollingFile>
|
||||||
|
|
||||||
|
<Console name="console" target="SYSTEM_OUT">
|
||||||
|
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} - %msg%n" />
|
||||||
|
</Console>
|
||||||
|
</Appenders>
|
||||||
|
<Loggers>
|
||||||
|
<Logger name="de.begerad" level="debug" additivity="true">
|
||||||
|
<appender-ref ref="rollingFile" level="debug" />
|
||||||
|
</Logger>
|
||||||
|
<Root level="debug" additivity="false">
|
||||||
|
<appender-ref ref="console" />
|
||||||
|
</Root>
|
||||||
|
</Loggers>
|
||||||
|
|
||||||
|
</Configuration>
|
|
@ -0,0 +1,38 @@
|
||||||
|
package de.begerad.fileagerm;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import static de.begerad.fileagerm.Main.LOG;
|
||||||
|
|
||||||
|
public class FileCleaner {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean-up files older than a certain time
|
||||||
|
*
|
||||||
|
* @param age age of files to be cleaned up
|
||||||
|
* @param dirPath directory path of files to be cleaned up
|
||||||
|
*/
|
||||||
|
public static void deleteFilesByAge(int age, String dirPath) {
|
||||||
|
|
||||||
|
File directory = new File(dirPath);
|
||||||
|
if (directory.exists()) {
|
||||||
|
|
||||||
|
File[] listFiles = FileExplorer.listFiles(dirPath);
|
||||||
|
LOG.debug("listFiles.length: {}", listFiles.length);
|
||||||
|
long purgeTime = System.currentTimeMillis() - (age * 24L * 60L * 60L * 1000L);
|
||||||
|
for (File listFile : listFiles) {
|
||||||
|
if (listFile.lastModified() < purgeTime) {
|
||||||
|
if (!listFile.delete()) {
|
||||||
|
LOG.warn("file could NOT be deleted: {}", listFile.getName());
|
||||||
|
} else {
|
||||||
|
LOG.debug("file deleted: {}", listFile.getName());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LOG.debug("file is too young to be deleted: {}", listFile.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LOG.warn("directory does not exist: {}", directory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
package de.begerad.fileagerm;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class FileExplorer {
|
||||||
|
private final String directory;
|
||||||
|
|
||||||
|
public FileExplorer(String directory) {
|
||||||
|
this.directory = directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return set containing list of file paths
|
||||||
|
*/
|
||||||
|
public Set<String> listFilesUsingJavaIO() {
|
||||||
|
return Stream.of(new File(directory).listFiles())
|
||||||
|
.filter(file -> !file.isDirectory())
|
||||||
|
.map(File::getName)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array containing list of File objects
|
||||||
|
*/
|
||||||
|
public File[] listFilesUsingFileFilter() {
|
||||||
|
//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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,37 +8,13 @@ import java.io.IOException;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static de.begerad.fileagerm.FileCleaner.deleteFilesByAge;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static String dirPath = "";
|
public static String dirPath = "";
|
||||||
|
|
||||||
public final static Logger LOG = LoggerFactory.getLogger(Main.class);
|
public final static Logger LOG = LoggerFactory.getLogger(Main.class);
|
||||||
|
|
||||||
public static void deleteFilesOlderThanNdays(int daysBack, String dirWay) {
|
|
||||||
|
|
||||||
File directory = new File(dirWay);
|
|
||||||
if (directory.exists()) {
|
|
||||||
|
|
||||||
//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.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: {}", listFile.getName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Main.LOG.warn("directory does not exist: {}", directory);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
LOG.debug("main() started...");
|
LOG.debug("main() started...");
|
||||||
|
|
||||||
|
@ -64,7 +40,7 @@ public class Main {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
FileList fileList = new FileList(dirPath);
|
FileExplorer fileList = new FileExplorer(dirPath);
|
||||||
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) {
|
||||||
|
@ -97,7 +73,7 @@ public class Main {
|
||||||
purgeTime = cal.getTimeInMillis();
|
purgeTime = cal.getTimeInMillis();
|
||||||
LOG.debug("age in ms: {}", purgeTime);
|
LOG.debug("age in ms: {}", purgeTime);
|
||||||
|
|
||||||
deleteFilesOlderThanNdays(age, dirPath);
|
deleteFilesByAge(age, dirPath);
|
||||||
|
|
||||||
LOG.debug("main() done.");
|
LOG.debug("main() done.");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue