find and copy files

This commit is contained in:
2015-12-08 22:16:11 +01:00
parent 3228588248
commit 0f14968bfe
7 changed files with 201 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
+1
View File
@@ -0,0 +1 @@
/target/
+23
View File
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>scripting</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
+5
View File
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
+4
View File
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
+43
View File
@@ -0,0 +1,43 @@
<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>scripting</groupId>
<artifactId>scripting</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.martiansoftware</groupId>
<artifactId>jsap</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,99 @@
package de.tilman.scripting;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPException;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Switch;
public class FindAndCopyFilesFromList {
private static Logger log = Logger.getLogger(FindAndCopyFilesFromList.class);
private void processFile(String fileList, String searchDir, String targetDir) throws IOException {
// read file list
List<String> lines = FileUtils.readLines(new File(fileList), "utf-8");
// search files
long dirs = 0, files = 0, copiedFiles = 0;
Iterator<File> it = FileUtils.iterateFilesAndDirs(new File(searchDir), TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
while (it.hasNext()) {
File file = it.next();
if (file.isDirectory())
dirs++;
else {
files++;
if (lines.contains(file.getName())) {
FileUtils.copyFile(file, new File(targetDir));
copiedFiles++;
}
}
}
System.out.println("Copied " + copiedFiles + " files from " + dirs + " directories with " + files + " files.");
}
public static void main(String[] args) {
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%p %m%n"))); // "%d{ISO8601} - %p %m%n"
log.setLevel(Level.INFO);
JSAP jsap = new JSAP();
try {
FlaggedOption fileListOption = new FlaggedOption("file list").setLongFlag("fileList").setShortFlag('l')
.setStringParser(JSAP.STRING_PARSER).setRequired(true);
fileListOption.setHelp("path to file list");
jsap.registerParameter(fileListOption);
FlaggedOption searchDirOption = new FlaggedOption("search dir").setLongFlag("searchDir").setShortFlag('s')
.setStringParser(JSAP.STRING_PARSER).setRequired(true);
searchDirOption.setHelp("directory to search");
jsap.registerParameter(searchDirOption);
FlaggedOption targetDirOption = new FlaggedOption("target dir").setLongFlag("targetDir").setShortFlag('t')
.setStringParser(JSAP.STRING_PARSER).setRequired(true);
targetDirOption.setHelp("directory to copy to");
jsap.registerParameter(targetDirOption);
Switch helpSwitch = new Switch("help").setLongFlag("help").setShortFlag('?');
helpSwitch.setHelp("print help and exit");
jsap.registerParameter(helpSwitch);
} catch (JSAPException e) {
log.fatal(e.getMessage());
System.exit(-1001);
}
JSAPResult config = jsap.parse(args);
if (!config.success() || config.getBoolean("help")) {
for (@SuppressWarnings("rawtypes")
java.util.Iterator errs = config.getErrorMessageIterator(); errs.hasNext();) {
log.error("Error: " + errs.next());
}
log.error("Usage: " + jsap.getUsage() + "\n\n" + jsap.getHelp());
System.exit(-1002);
}
FindAndCopyFilesFromList facffl = new FindAndCopyFilesFromList();
try {
facffl.processFile(config.getString("file list"), config.getString("search dir"), config.getString("target dir"));
} catch (IOException e) {
e.printStackTrace();
}
}
}