2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
10 package eu.svjatoslav.meviz.renamer;
14 import eu.svjatoslav.commons.string.WildCardMatcher;
15 import eu.svjatoslav.meviz.Module;
17 public class Main implements Module {
19 CommandlineHandler commandlineHandler = new CommandlineHandler();
21 public int processedFilesCount;
23 RenamingOptions options;
26 * Verify that program has enough information to start executing
28 * @return true if all is ok, false if errors were found
30 public boolean checkThatThereIsEnoughDataToProceed() {
31 if (options.inputPatterns.size() == 0) {
32 System.out.println("Error: no input patterns given.");
36 if (options.outputPattern == null) {
37 System.out.println("Error: no output pattern given.");
44 public boolean fileMatchesInputPattern(final File file) {
45 final String fileName = file.getName();
47 for (final String inputPattern : options.inputPatterns)
48 if (WildCardMatcher.match(fileName, inputPattern))
55 public String getDescription() {
56 return "Mass rename files according to given pattern.";
60 public String getModuleCommand() {
64 public void handleFile(final File file) {
66 System.out.println("Error: file \"" + file.getAbsolutePath()
67 + "\" does not exist.");
71 if (file.isDirectory()) {
72 for (final File subFile : file.listFiles())
73 if (subFile.isDirectory()) {
74 if (options.recursive)
78 } else if (file.isFile())
79 if (fileMatchesInputPattern(file)) {
81 final StringBuffer targetFilePath = new StringBuffer();
83 targetFilePath.append(file.getParent());
84 targetFilePath.append("/");
86 for (int i = 0; i < options.outputPattern.length(); i++) {
88 final char c = options.outputPattern.charAt(i);
91 targetFilePath.append(file.getName());
94 final String processedFileCountString = String
95 .valueOf(processedFilesCount);
97 for (int j = 0; j < (5 - processedFileCountString
99 targetFilePath.append(0);
101 targetFilePath.append(processedFileCountString);
102 processedFilesCount++;
104 targetFilePath.append(" ");
106 targetFilePath.append(c);
109 final File targetFile = new File(targetFilePath.toString());
111 if (!options.testOnly) {
113 if (targetFile.exists())
115 .println("Renaming aborted because target file: "
116 + targetFile.getAbsolutePath()
117 + " already exists.");
119 file.renameTo(targetFile);
122 System.out.println("About to rename file: "
123 + file.getAbsolutePath());
124 System.out.println("into: " + targetFile.getAbsolutePath());
130 public void rename() {
131 handleFile(options.targetDirectory);
135 public void run(final String[] args) {
137 options = commandlineHandler.parseCommandlineArguments(args);
139 if (options == null) {
140 showCommandlineHelp();
144 if (checkThatThereIsEnoughDataToProceed()) {
146 System.out.println("Renaming using input patterns: ");
147 for (final String inputPattern : options.inputPatterns)
148 System.out.println(" " + inputPattern);
152 showCommandlineHelp();
156 public void showCommandlineHelp() {
157 commandlineHandler.initParser().showHelp();
158 System.out.println("");
159 System.out.println("Output pattern special symbols:");
160 System.out.println(" % - file number");
161 System.out.println(" * - original file name");