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;
13 import java.util.Arrays;
15 import eu.svjatoslav.commons.string.WildCardMatcher;
16 import eu.svjatoslav.meviz.Module;
18 public class Main implements Module {
20 CommandlineHandler commandlineHandler = new CommandlineHandler();
22 public int processedFilesCount;
24 RenamingOptions options;
27 * Verify that program has enough information to start executing
29 * @return true if all is ok, false if errors were found
31 public boolean checkThatThereIsEnoughDataToProceed() {
32 if (options.inputPatterns.size() == 0) {
33 System.out.println("Error: no input patterns given.");
37 if (options.outputPattern == null) {
38 System.out.println("Error: no output pattern given.");
45 public boolean fileMatchesInputPattern(final File file) {
46 final String fileName = file.getName();
48 for (final String inputPattern : options.inputPatterns)
49 if (WildCardMatcher.match(fileName, inputPattern))
56 public String getDescription() {
57 return "Mass rename files according to given pattern.";
61 public String getModuleCommand() {
65 private void processDirectory(final File directory) {
66 final File[] directoryContents = directory.listFiles();
68 // sort directory contents alphabetically
69 Arrays.sort(directoryContents);
71 for (final File subFile : directoryContents)
72 if (subFile.isDirectory()) {
73 if (options.recursive)
74 processFileOrDirectory(subFile);
76 processFileOrDirectory(subFile);
79 private void processFile(final File file) {
80 final StringBuffer targetFilePath = new StringBuffer();
82 targetFilePath.append(file.getParent());
83 targetFilePath.append("/");
85 for (int i = 0; i < options.outputPattern.length(); i++) {
87 final char c = options.outputPattern.charAt(i);
90 targetFilePath.append(file.getName());
93 final String processedFileCountString = String
94 .valueOf(processedFilesCount);
96 for (int j = 0; j < (5 - processedFileCountString.length()); j++)
97 targetFilePath.append(0);
99 targetFilePath.append(processedFileCountString);
100 processedFilesCount++;
102 targetFilePath.append(" ");
104 targetFilePath.append(c);
107 final File targetFile = new File(targetFilePath.toString());
109 if (!options.testOnly) {
111 if (targetFile.exists())
112 System.out.println("Renaming aborted because target file: "
113 + targetFile.getAbsolutePath() + " already exists.");
115 file.renameTo(targetFile);
118 System.out.println("About to rename file: "
119 + file.getAbsolutePath());
120 System.out.println("into: " + targetFile.getAbsolutePath());
124 public void processFileOrDirectory(final File file) {
125 if (!file.exists()) {
126 System.out.println("Error: file \"" + file.getAbsolutePath()
127 + "\" does not exist.");
131 if (file.isDirectory())
132 processDirectory(file);
133 else if (file.isFile())
134 if (fileMatchesInputPattern(file))
139 public void rename() {
140 processFileOrDirectory(options.targetDirectory);
144 public void run(final String[] args) {
146 options = commandlineHandler.parseCommandlineArguments(args);
148 if (options == null) {
149 showCommandlineHelp();
153 if (checkThatThereIsEnoughDataToProceed()) {
155 System.out.println("Renaming using input patterns: ");
156 for (final String inputPattern : options.inputPatterns)
157 System.out.println(" " + inputPattern);
161 showCommandlineHelp();
165 public void showCommandlineHelp() {
166 commandlineHandler.initParser().showHelp();
167 System.out.println("");
168 System.out.println("Output pattern special symbols:");
169 System.out.println(" % - file number");
170 System.out.println(" * - original file name");