From 0f14968bfe3c1973f7deed0700e4d06ee2eb0e64 Mon Sep 17 00:00:00 2001 From: Tilman Liero Date: Tue, 8 Dec 2015 22:16:11 +0100 Subject: [PATCH] find and copy files --- .classpath | 26 +++++ .gitignore | 1 + .project | 23 +++++ .settings/org.eclipse.jdt.core.prefs | 5 + .settings/org.eclipse.m2e.core.prefs | 4 + pom.xml | 43 ++++++++ .../scripting/FindAndCopyFilesFromList.java | 99 +++++++++++++++++++ 7 files changed, 201 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 pom.xml create mode 100644 src/main/java/de/tilman/scripting/FindAndCopyFilesFromList.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..83dd2f4 --- /dev/null +++ b/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/.project b/.project new file mode 100644 index 0000000..b696ed1 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + scripting + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..714351a --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..3613e04 --- /dev/null +++ b/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + scripting + scripting + 0.0.1-SNAPSHOT + + + + com.martiansoftware + jsap + 2.1 + + + log4j + log4j + 1.2.16 + + + org.apache.commons + commons-lang3 + 3.3.2 + + + commons-io + commons-io + 2.4 + + + + + + + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + + + + \ No newline at end of file diff --git a/src/main/java/de/tilman/scripting/FindAndCopyFilesFromList.java b/src/main/java/de/tilman/scripting/FindAndCopyFilesFromList.java new file mode 100644 index 0000000..7ce9573 --- /dev/null +++ b/src/main/java/de/tilman/scripting/FindAndCopyFilesFromList.java @@ -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 lines = FileUtils.readLines(new File(fileList), "utf-8"); + + // search files + long dirs = 0, files = 0, copiedFiles = 0; + + Iterator 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(); + } + } + +}