2 * Meviz - Various tools collection to work with multimedia. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
7 package eu.svjatoslav.meviz.renamer;
9 import eu.svjatoslav.commons.string.GlobMatcher;
10 import eu.svjatoslav.meviz.Module;
13 import java.util.Arrays;
15 public class Main implements Module {
17 private final CommandlineHandler commandlineHandler = new CommandlineHandler();
18 private int processedFilesCount;
19 private RenamingOptions options;
22 * Verify that program has enough information to start executing
24 * @return true if all is ok, false if errors were found
26 private boolean checkThatThereIsEnoughDataToProceed() {
27 if (options.inputPatterns.size() == 0) {
28 System.out.println("Error: no input patterns given.");
32 if (options.outputPattern == null) {
33 System.out.println("Error: no output pattern given.");
40 private boolean fileMatchesInputPattern(final File file) {
41 final String fileName = file.getName();
43 for (final String inputPattern : options.inputPatterns)
44 if (GlobMatcher.match(fileName, inputPattern))
51 public String getDescription() {
52 return "Mass rename files according to given pattern.";
56 public String getModuleCommand() {
60 private void processDirectory(final File directory) {
61 final File[] directoryContents = directory.listFiles();
63 // sort directory contents alphabetically
64 Arrays.sort(directoryContents);
66 for (final File subFile : directoryContents)
67 if (subFile.isDirectory()) {
68 if (options.recursive)
69 processFileOrDirectory(subFile);
71 processFileOrDirectory(subFile);
74 private void processFile(final File file) {
75 final StringBuilder targetFilePath = new StringBuilder();
77 targetFilePath.append(file.getParent());
78 targetFilePath.append("/");
80 for (int i = 0; i < options.outputPattern.length(); i++) {
82 final char c = options.outputPattern.charAt(i);
85 targetFilePath.append(file.getName());
88 final String processedFileCountString = String
89 .valueOf(processedFilesCount);
91 for (int j = 0; j < (5 - processedFileCountString.length()); j++)
92 targetFilePath.append(0);
94 targetFilePath.append(processedFileCountString);
95 processedFilesCount++;
97 targetFilePath.append(" ");
99 targetFilePath.append(c);
102 final File targetFile = new File(targetFilePath.toString());
104 if (!options.testOnly) {
106 if (targetFile.exists())
107 System.out.println("Renaming aborted because target file: "
108 + targetFile.getAbsolutePath() + " already exists.");
110 file.renameTo(targetFile);
113 System.out.println("About to rename file: "
114 + file.getAbsolutePath());
115 System.out.println("into: " + targetFile.getAbsolutePath());
119 private void processFileOrDirectory(final File file) {
120 if (!file.exists()) {
121 System.out.println("Error: file \"" + file.getAbsolutePath()
122 + "\" does not exist.");
126 if (file.isDirectory())
127 processDirectory(file);
128 else if (file.isFile())
129 if (fileMatchesInputPattern(file))
134 private void rename() {
135 processFileOrDirectory(options.targetDirectory);
139 public void run(final String[] args) {
141 options = commandlineHandler.parseCommandlineArguments(args);
143 if (options == null) {
144 showCommandlineHelp();
148 if (checkThatThereIsEnoughDataToProceed()) {
150 System.out.println("Renaming using input patterns: ");
151 for (final String inputPattern : options.inputPatterns)
152 System.out.println(" " + inputPattern);
156 showCommandlineHelp();
160 public void showCommandlineHelp() {
161 commandlineHandler.parser.showHelp();
162 System.out.println("");
163 System.out.println("Output pattern special symbols:");
164 System.out.println(" % - file number");
165 System.out.println(" * - original file name");