feat(files-age-rm): inital commit
This commit is contained in:
commit
fafd16eb8c
|
@ -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,43 @@
|
|||
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 FileList {
|
||||
private final String directory;
|
||||
|
||||
public FileList(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 names
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
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;
|
||||
|
||||
public class Main {
|
||||
public static String dirPath = "";
|
||||
|
||||
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();
|
||||
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);
|
||||
} else {
|
||||
|
||||
}
|
||||
} else {
|
||||
Main.LOG.debug("file is too young to be deleted");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Main.LOG.warn("directory does not exist");
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
FileList fileList = new FileList(dirPath);
|
||||
File[] list = fileList.listFilesUsingFileFilter();
|
||||
LOG.debug("List of files in the specified directory:");
|
||||
if (list != null) {
|
||||
for (File fileName : list) {
|
||||
LOG.debug("{}", fileName);
|
||||
}
|
||||
} 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);
|
||||
|
||||
deleteFilesOlderThanNdays(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>
|
Loading…
Reference in New Issue