PhotoRename

PhotoRename is a simple program to rename photos by their date. It is setup to work with files that begin with DSC. For example if you have a directory of photos that have names like DSC12345678.jpg where the file date is August 1st 2004, the program will rename the files to 20040801_DSC12345678.jpg. Many times you'll end up with several thousand photos from a digital camera. When you resize or edit the photos most programs change the file date to the current date and time. This can make it very difficult to locate photos chronologically. Naming files based on their date makes certian that the date information will never be lost--even if you change photo management software.

import java.io.*;
import java.text.*;
import java.util.*;
public class PhotoRename
{
  
  /**
    * renames all files in the current directory with the date as a prefix
    * Only works on files beginning with DSCN.  Renames to
    * 20010125_DSCN1234.jpg format.
    */
  public static void main(String [] args) {
    int yearInt;
    int dateInt;
    int monthInt;
    
    String yearString;
    String dateString;
    String monthString;
    
    //create a DateFormat instance
    DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
    
    //get an array of all the files in the current
    //directory
    File [] files = new File(".").listFiles();
    
    forint i = 0; i < files.length; i++) {
      File file = files[i];
      Date date = new Date(file.lastModified());
      Calendar c = new GregorianCalendar();
      String fileName = file.getName();
      String fullDateString;
      c.setTime(date);
      
      System.out.println(files[i].getName());
      
      yearInt  = c.get(Calendar.YEAR);
      dateInt  = c.get(Calendar.DAY_OF_MONTH);
      monthInt = c.get(Calendar.MONTH1;
      
      yearString = "" + yearInt;
      
      if(dateInt < 10) {
        dateString = "0" + dateInt;
      }
      else {
        dateString = "" + dateInt;
      }
      
      if(monthInt < 10monthString = "0" + monthInt;
      else monthString = "" + monthInt;
      
      System.out.println(df.format(date));
      
      fullDateString = yearString + monthString +dateString +"_";
      
      if(fileName.startsWith("DSC")) {
        file.renameTo(new File(fullDateString+fileName));
      }
      
      fileName = file.getName();
      if(fileName.indexOf(' '0) {
        file.renameTo(new File(fileName.replace(' ''_')));
      }
      
    }
  }
}