From 6ac012ad5d2a3174af86a5374aafedd0a8aec5ee Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 23 Feb 2012 05:01:06 +0000 Subject: [PATCH] * added copyTo() to scripting api --- .../filebot/cli/ScriptShell.lib.groovy | 1 + .../net/sourceforge/tuned/FileUtilities.java | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy index b20f478a..deb649a5 100644 --- a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy +++ b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy @@ -38,6 +38,7 @@ File.metaClass.isDerived = { f -> isDerived(delegate, f) } File.metaClass.validateFileName = { validateFileName(delegate) } File.metaClass.validateFilePath = { validateFilePath(delegate) } File.metaClass.moveTo = { f -> moveRename(delegate, f instanceof File ? f : new File(f.toString())) } +File.metaClass.copyTo = { dir -> copyAs(delegate, new File(dir, delegate.getName())) } List.metaClass.mapByFolder = { mapByFolder(delegate) } List.metaClass.mapByExtension = { mapByExtension(delegate) } String.metaClass.getExtension = { getExtension(delegate) } diff --git a/source/net/sourceforge/tuned/FileUtilities.java b/source/net/sourceforge/tuned/FileUtilities.java index 9e0d3add..45204bb3 100644 --- a/source/net/sourceforge/tuned/FileUtilities.java +++ b/source/net/sourceforge/tuned/FileUtilities.java @@ -92,6 +92,31 @@ public final class FileUtilities { } + public static File copyAs(File source, File destination) throws IOException { + // resolve destination + if (!destination.isAbsolute()) { + // same folder, different name + destination = new File(source.getParentFile(), destination.getPath()); + } + + // make sure we that we can create the destination folder structure + File destinationFolder = destination.getParentFile(); + + // create parent folder if necessary + if (!destinationFolder.isDirectory() && !destinationFolder.mkdirs()) { + throw new IOException("Failed to create folder: " + destinationFolder); + } + + if (source.isDirectory()) { // copy folder + org.apache.commons.io.FileUtils.copyDirectory(source, destination); + } else { // copy file + org.apache.commons.io.FileUtils.copyFile(source, destination); + } + + return destination; + } + + public static byte[] readFile(File source) throws IOException { InputStream in = new FileInputStream(source);