X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fmeviz%2Frenamer%2FMain.java;h=6495e0f3bd9538e6852f91c3ed694fb3f06dd57a;hb=1b742640af99284aae1cd10c259254137316ba7c;hp=8818889040fcb8167848a7a25d5330e44c1c62a5;hpb=6cda75011fa4346a3cc2c7c7790eb1f1b12cd3fe;p=meviz.git diff --git a/src/main/java/eu/svjatoslav/meviz/renamer/Main.java b/src/main/java/eu/svjatoslav/meviz/renamer/Main.java index 8818889..6495e0f 100755 --- a/src/main/java/eu/svjatoslav/meviz/renamer/Main.java +++ b/src/main/java/eu/svjatoslav/meviz/renamer/Main.java @@ -9,165 +9,163 @@ package eu.svjatoslav.meviz.renamer; -import java.io.File; -import java.util.Arrays; - import eu.svjatoslav.commons.string.WildCardMatcher; import eu.svjatoslav.meviz.Module; -public class Main implements Module { - - CommandlineHandler commandlineHandler = new CommandlineHandler(); - - public int processedFilesCount; - - RenamingOptions options; - - /** - * Verify that program has enough information to start executing - * - * @return true if all is ok, false if errors were found - */ - public boolean checkThatThereIsEnoughDataToProceed() { - if (options.inputPatterns.size() == 0) { - System.out.println("Error: no input patterns given."); - return false; - } - - if (options.outputPattern == null) { - System.out.println("Error: no output pattern given."); - return false; - } - - return true; - } - - public boolean fileMatchesInputPattern(final File file) { - final String fileName = file.getName(); - - for (final String inputPattern : options.inputPatterns) - if (WildCardMatcher.match(fileName, inputPattern)) - return true; +import java.io.File; +import java.util.Arrays; - return false; - } +public class Main implements Module { - @Override - public String getDescription() { - return "Mass rename files according to given pattern."; - } + public int processedFilesCount; + CommandlineHandler commandlineHandler = new CommandlineHandler(); + RenamingOptions options; + + /** + * Verify that program has enough information to start executing + * + * @return true if all is ok, false if errors were found + */ + public boolean checkThatThereIsEnoughDataToProceed() { + if (options.inputPatterns.size() == 0) { + System.out.println("Error: no input patterns given."); + return false; + } + + if (options.outputPattern == null) { + System.out.println("Error: no output pattern given."); + return false; + } + + return true; + } + + public boolean fileMatchesInputPattern(final File file) { + final String fileName = file.getName(); - @Override - public String getModuleCommand() { - return "rename"; - }; + for (final String inputPattern : options.inputPatterns) + if (WildCardMatcher.match(fileName, inputPattern)) + return true; + + return false; + } - private void processDirectory(final File directory) { - final File[] directoryContents = directory.listFiles(); + @Override + public String getDescription() { + return "Mass rename files according to given pattern."; + } - // sort directory contents alphabetically - Arrays.sort(directoryContents); - - for (final File subFile : directoryContents) - if (subFile.isDirectory()) { - if (options.recursive) - processFileOrDirectory(subFile); - } else - processFileOrDirectory(subFile); - } - - private void processFile(final File file) { - final StringBuffer targetFilePath = new StringBuffer(); - - targetFilePath.append(file.getParent()); - targetFilePath.append("/"); - - for (int i = 0; i < options.outputPattern.length(); i++) { - - final char c = options.outputPattern.charAt(i); - - if (c == '*') - targetFilePath.append(file.getName()); - else if (c == '%') { - - final String processedFileCountString = String - .valueOf(processedFilesCount); - - for (int j = 0; j < (5 - processedFileCountString.length()); j++) - targetFilePath.append(0); - - targetFilePath.append(processedFileCountString); - processedFilesCount++; - } else if (c == '_') - targetFilePath.append(" "); - else - targetFilePath.append(c); - } - - final File targetFile = new File(targetFilePath.toString()); - - if (!options.testOnly) { - - if (targetFile.exists()) - System.out.println("Renaming aborted because target file: " - + targetFile.getAbsolutePath() + " already exists."); - else - file.renameTo(targetFile); - - } else { - System.out.println("About to rename file: " - + file.getAbsolutePath()); - System.out.println("into: " + targetFile.getAbsolutePath()); - } - } - - public void processFileOrDirectory(final File file) { - if (!file.exists()) { - System.out.println("Error: file \"" + file.getAbsolutePath() - + "\" does not exist."); - return; - } - - if (file.isDirectory()) - processDirectory(file); - else if (file.isFile()) - if (fileMatchesInputPattern(file)) - processFile(file); - - } - - public void rename() { - processFileOrDirectory(options.targetDirectory); - } - - @Override - public void run(final String[] args) { - - options = commandlineHandler.parseCommandlineArguments(args); - - if (options == null) { - showCommandlineHelp(); - return; - } - - if (checkThatThereIsEnoughDataToProceed()) { - - System.out.println("Renaming using input patterns: "); - for (final String inputPattern : options.inputPatterns) - System.out.println(" " + inputPattern); - - rename(); - } else - showCommandlineHelp(); - } - - @Override - public void showCommandlineHelp() { - commandlineHandler.parser.showHelp(); - System.out.println(""); - System.out.println("Output pattern special symbols:"); - System.out.println(" % - file number"); - System.out.println(" * - original file name"); - } + @Override + public String getModuleCommand() { + return "rename"; + } + + private void processDirectory(final File directory) { + final File[] directoryContents = directory.listFiles(); + + // sort directory contents alphabetically + Arrays.sort(directoryContents); + + for (final File subFile : directoryContents) + if (subFile.isDirectory()) { + if (options.recursive) + processFileOrDirectory(subFile); + } else + processFileOrDirectory(subFile); + } + + private void processFile(final File file) { + final StringBuilder targetFilePath = new StringBuilder(); + + targetFilePath.append(file.getParent()); + targetFilePath.append("/"); + + for (int i = 0; i < options.outputPattern.length(); i++) { + + final char c = options.outputPattern.charAt(i); + + if (c == '*') + targetFilePath.append(file.getName()); + else if (c == '%') { + + final String processedFileCountString = String + .valueOf(processedFilesCount); + + for (int j = 0; j < (5 - processedFileCountString.length()); j++) + targetFilePath.append(0); + + targetFilePath.append(processedFileCountString); + processedFilesCount++; + } else if (c == '_') + targetFilePath.append(" "); + else + targetFilePath.append(c); + } + + final File targetFile = new File(targetFilePath.toString()); + + if (!options.testOnly) { + + if (targetFile.exists()) + System.out.println("Renaming aborted because target file: " + + targetFile.getAbsolutePath() + " already exists."); + else + file.renameTo(targetFile); + + } else { + System.out.println("About to rename file: " + + file.getAbsolutePath()); + System.out.println("into: " + targetFile.getAbsolutePath()); + } + } + + public void processFileOrDirectory(final File file) { + if (!file.exists()) { + System.out.println("Error: file \"" + file.getAbsolutePath() + + "\" does not exist."); + return; + } + + if (file.isDirectory()) + processDirectory(file); + else if (file.isFile()) + if (fileMatchesInputPattern(file)) + processFile(file); + + } + + public void rename() { + processFileOrDirectory(options.targetDirectory); + } + + @Override + public void run(final String[] args) { + + options = commandlineHandler.parseCommandlineArguments(args); + + if (options == null) { + showCommandlineHelp(); + return; + } + + if (checkThatThereIsEnoughDataToProceed()) { + + System.out.println("Renaming using input patterns: "); + for (final String inputPattern : options.inputPatterns) + System.out.println(" " + inputPattern); + + rename(); + } else + showCommandlineHelp(); + } + + @Override + public void showCommandlineHelp() { + commandlineHandler.parser.showHelp(); + System.out.println(""); + System.out.println("Output pattern special symbols:"); + System.out.println(" % - file number"); + System.out.println(" * - original file name"); + } }