import java.io.*; import java.util.*; import java.text.*; public class DateRename { private File directory, outputFile; private FileOutputStream outputWriter; private Object[] files; public DateRename(File dir, String prefix, String postfix) { this.directory = dir; outputFile = new File(dir, "output.txt"); try { outputWriter = new FileOutputStream(outputFile); } catch(IOException ioe) { System.out.println("Output file error"); } if (dir.isDirectory()) files = directory.listFiles(); files = sort(files); // Sort BY DATE for (int i=0; i < files.length; i++) { File temp = (File)(files[i]); long lastModified = temp.lastModified(); //Date lastModifiedDate = new Date(lastModified); //DateFormat myDateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM); if (temp.renameTo(new File(dir, prefix + i + postfix))) System.out.println("Renaming: " + temp + "\t To: " + prefix + i + postfix); temp.setLastModified(lastModified); /* String fileline = "\""\n"; System.out.println(fileline); byte[] out = fileline.getBytes(); try { outputWriter.write(out); } catch(IOException ioe2) { System.out.println("Output file error"); } */ } } public Object[] sort(Object[] array) { return mergeSort(array); } // ------------------------------------------------ // Merge sort // ------------------------------------------------ public Object[] mergeSort(Object[] arrayToSort) { // Set up the main temporary array and start with partitions of size 1. Object[] tempArray = new Object[arrayToSort.length]; int partitionLength = 1; int arrayIndex; // Keep merging the elements until the partion length is longer // than the array itself. while (partitionLength <= arrayToSort.length) { // Reset for each loop arrayIndex = 0; // Keep moving through the remaining groups of partitions until // the end of array is hit. while (arrayIndex < arrayToSort.length) { if (arrayToSort.length-arrayIndex > partitionLength) { // While we have values left, we'll load them into two arrays // (with their associated row of Objects[]), merge them, and // add them to the temporary Object table. // We have to figure out the length of the second array // because we may be coming to the end of the main array // and it might not be a whole partition length long. int lengthOfSecond; if (arrayToSort.length-arrayIndex-partitionLength > partitionLength) lengthOfSecond = partitionLength; else lengthOfSecond = arrayToSort.length-arrayIndex-partitionLength; // Set up arrays Object[] tempArray1 = new Object[partitionLength]; Object[] tempArray2 = new Object[lengthOfSecond]; // Load up partition elements for (int i=0; i < partitionLength; i++) { tempArray1[i] = arrayToSort[arrayIndex+i]; if (i < lengthOfSecond) tempArray2[i] = arrayToSort[arrayIndex+i+partitionLength]; } // Merge, giving which column and how to sort Object[] mergedArray = mergeArraysByCol(tempArray1, tempArray2); // Load merged array into main temporary array for (int i=0; i < mergedArray.length; i++) tempArray[arrayIndex+i] = mergedArray[i]; } else { // Pick up any remaining elements aren't long enough to fill a // single partition. Copy them straight to the tempArray, they // will be picked up on a later merge. for (int i=0; i < arrayToSort.length-arrayIndex; i++) tempArray[arrayIndex+i] = arrayToSort[arrayIndex+i]; } // Move the place in the array up to the next pair (or partial) arrayIndex+=partitionLength*2; } // Swap the new results back to be partitioned and merged again. arrayToSort = tempArray; partitionLength*=2; } return arrayToSort; } // ------------------------------------------------ // mergeArraysByCol // Array merging function // Merges two sorted Object[][] arrays into a larger, sorted one // // Sorting depends on specified column and sort order // ------------------------------------------------ public Object[] mergeArraysByCol(Object[] array1, Object[] array2) { // Set up the final merged array Object[] arrayMerged = new Object[array1.length+array2.length]; // These indexes hold the loop's current position in that array int array1Index = 0, array2Index = 0; for (int i=0; i < arrayMerged.length; i++) { // See if both arrays still have elements remaining if (array1Index < array1.length && array2Index < array2.length) { // WORKS WITH DATERENAME ONLY int result = 0; File tmp1 = (File)array1[array1Index]; File tmp2 = (File)array2[array2Index]; if (tmp1.lastModified() < tmp2.lastModified()) { result = -1; } else { result = 1; } // WORKS WITH DATERENAME ONLY // Depending on the test, copy the rightful row to the merged array. // Move to the next position in the array to test if (result < 0) { arrayMerged[i] = array1[array1Index]; array1Index++; } else { arrayMerged[i] = array2[array2Index]; array2Index++; } } else if (array1Index == array1.length) { // If one of the arrays has been exhausted, we find which one // and add the rest of the other to the merged array arrayMerged[i] = array2[array2Index]; array2Index++; } else { arrayMerged[i] = array1[array1Index]; array1Index++; } } return arrayMerged; } public static void main(String args[]) { char singleBSlash = '\\'; args[0].replace(singleBSlash, File.separatorChar); DateRename dr = new DateRename(new File(args[0]), args[1], args[2]); } }