2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012 -- 2018, 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;
12 import eu.svjatoslav.commons.string.WildCardMatcher;
13 import eu.svjatoslav.meviz.Module;
16 import java.util.Arrays;
18 public class Main implements Module {
20 private final CommandlineHandler commandlineHandler = new CommandlineHandler();
21 private int processedFilesCount;
22 private RenamingOptions options;
25 * Verify that program has enough information to start executing
27 * @return true if all is ok, false if errors were found
29 private boolean checkThatThereIsEnoughDataToProceed() {
30 if (options.inputPatterns.size() == 0) {
31 System.out.println("Error: no input patterns given.");
35 if (options.outputPattern == null) {
36 System.out.println("Error: no output pattern given.");
43 private boolean fileMatchesInputPattern(final File file) {
44 final String fileName = file.getName();
46 for (final String inputPattern : options.inputPatterns)
47 if (WildCardMatcher.match(fileName, inputPattern))
54 public String getDescription() {
55 return "Mass rename files according to given pattern.";
59 public String getModuleCommand() {
63 private void processDirectory(final File directory) {
64 final File[] directoryContents = directory.listFiles();
66 // sort directory contents alphabetically
67 Arrays.sort(directoryContents);
69 for (final File subFile : directoryContents)
70 if (subFile.isDirectory()) {
71 if (options.recursive)
72 processFileOrDirectory(subFile);
74 processFileOrDirectory(subFile);
77 private void processFile(final File file) {
78 final StringBuilder targetFilePath = new StringBuilder();
80 targetFilePath.append(file.getParent());
81 targetFilePath.append("/");
83 for (int i = 0; i < options.outputPattern.length(); i++) {
85 final char c = options.outputPattern.charAt(i);
88 targetFilePath.append(file.getName());
91 final String processedFileCountString = String
92 .valueOf(processedFilesCount);
94 for (int j = 0; j < (5 - processedFileCountString.length()); j++)
95 targetFilePath.append(0);
97 targetFilePath.append(processedFileCountString);
98 processedFilesCount++;
100 targetFilePath.append(" ");
102 targetFilePath.append(c);
105 final File targetFile = new File(targetFilePath.toString());
107 if (!options.testOnly) {
109 if (targetFile.exists())
110 System.out.println("Renaming aborted because target file: "
111 + targetFile.getAbsolutePath() + " already exists.");
113 file.renameTo(targetFile);
116 System.out.println("About to rename file: "
117 + file.getAbsolutePath());
118 System.out.println("into: " + targetFile.getAbsolutePath());
122 private void processFileOrDirectory(final File file) {
123 if (!file.exists()) {
124 System.out.println("Error: file \"" + file.getAbsolutePath()
125 + "\" does not exist.");
129 if (file.isDirectory())
130 processDirectory(file);
131 else if (file.isFile())
132 if (fileMatchesInputPattern(file))
137 private void rename() {
138 processFileOrDirectory(options.targetDirectory);
142 public void run(final String[] args) {
144 options = commandlineHandler.parseCommandlineArguments(args);
146 if (options == null) {
147 showCommandlineHelp();
151 if (checkThatThereIsEnoughDataToProceed()) {
153 System.out.println("Renaming using input patterns: ");
154 for (final String inputPattern : options.inputPatterns)
155 System.out.println(" " + inputPattern);
159 showCommandlineHelp();
163 public void showCommandlineHelp() {
164 commandlineHandler.parser.showHelp();
165 System.out.println("");
166 System.out.println("Output pattern special symbols:");
167 System.out.println(" % - file number");
168 System.out.println(" * - original file name");