package eu.svjatoslav.meviz.renamer;
import java.io.File;
+import java.util.Arrays;
import eu.svjatoslav.commons.string.WildCardMatcher;
import eu.svjatoslav.meviz.Module;
return "rename";
};
- public void handleFile(final File file) {
- if (!file.exists()) {
- System.out.println("Error: file \"" + file.getAbsolutePath()
- + "\" does not exist.");
- return;
- }
+ private void processDirectory(final File directory) {
+ final File[] directoryContents = directory.listFiles();
- if (file.isDirectory()) {
- for (final File subFile : file.listFiles())
- if (subFile.isDirectory()) {
- if (options.recursive)
- handleFile(subFile);
- } else
- handleFile(subFile);
- } else if (file.isFile())
- if (fileMatchesInputPattern(file)) {
+ // sort directory contents alphabetically
+ Arrays.sort(directoryContents);
- final StringBuffer targetFilePath = new StringBuffer();
+ 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("/");
+ targetFilePath.append(file.getParent());
+ targetFilePath.append("/");
- for (int i = 0; i < options.outputPattern.length(); i++) {
+ for (int i = 0; i < options.outputPattern.length(); i++) {
- final char c = options.outputPattern.charAt(i);
+ final char c = options.outputPattern.charAt(i);
- if (c == '*')
- targetFilePath.append(file.getName());
- else if (c == '%') {
+ if (c == '*')
+ targetFilePath.append(file.getName());
+ else if (c == '%') {
- final String processedFileCountString = String
- .valueOf(processedFilesCount);
+ final String processedFileCountString = String
+ .valueOf(processedFilesCount);
- for (int j = 0; j < (5 - processedFileCountString
- .length()); j++)
- targetFilePath.append(0);
+ 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);
+ }
- targetFilePath.append(processedFileCountString);
- processedFilesCount++;
- } else if (c == '_')
- targetFilePath.append(" ");
- else
- targetFilePath.append(c);
- }
+ final File targetFile = new File(targetFilePath.toString());
- final File targetFile = new File(targetFilePath.toString());
+ if (!options.testOnly) {
- if (!options.testOnly) {
+ if (targetFile.exists())
+ System.out.println("Renaming aborted because target file: "
+ + targetFile.getAbsolutePath() + " already exists.");
+ else
+ file.renameTo(targetFile);
- 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;
+ }
- } else {
- System.out.println("About to rename file: "
- + file.getAbsolutePath());
- System.out.println("into: " + targetFile.getAbsolutePath());
- }
- }
+ if (file.isDirectory())
+ processDirectory(file);
+ else if (file.isFile())
+ if (fileMatchesInputPattern(file))
+ processFile(file);
}
public void rename() {
- handleFile(options.targetDirectory);
+ processFileOrDirectory(options.targetDirectory);
}
@Override