handle files in alphabetical order
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 26 Jun 2013 07:38:38 +0000 (10:38 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 26 Jun 2013 07:38:38 +0000 (10:38 +0300)
src/main/java/eu/svjatoslav/meviz/renamer/Main.java

index fff4b39..3a15e03 100755 (executable)
@@ -10,6 +10,7 @@
 package eu.svjatoslav.meviz.renamer;
 
 import java.io.File;
+import java.util.Arrays;
 
 import eu.svjatoslav.commons.string.WildCardMatcher;
 import eu.svjatoslav.meviz.Module;
@@ -61,74 +62,82 @@ public class Main implements 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