HomeHome ImpressionImpressions IdiomsIdioms DeliciousDelicious Self-ImageSelf-Image ImpermanenceImpermanence Happy ShoppingHappy Shopping Do It YourselfDo It Yourself Street ArtStreet Art Tibetan ArtTibetan Art Magic NumberMagic Number Bread and CircusesBread and Circuses FeedbackFeedback ProjectProject
JavaScript Location Search/Hash-Params To Object-Map - Magic Number No. 26 Advent Calendar - Happy Shopping No. 34 Renaming DCIM-Folders to Foto Created Date - Magic Number No. 25 A Hundred Good Reasons - Impermanence No. 15 Delete Temporary Locked Files - Do It Yourself No. 42 Replacing Vowels - Idioms No. 8

Renaming DCIM-Folders to Foto Created Date - Magic Number No. 25

Sunday 2011-09-18 00:09:36   [feedback]   [link]

With the help of the ExifTool and the following Java application it is possible to automatically rename DCIM-folders to the date when the pictures where taken. The resulting format like 2011-09-18 allows better sorting than the usual format for folders by digital cameras like 101_180911.
To use this tool compile this class:

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RenameFoldersByPhotoCreatedDate {

	public static void main(String[] args) throws Exception {
		new RenameFoldersByPhotoCreatedDate().execute(new File(
				args.length > 0 ? args[0] : "."), Arrays.asList(args).contains(
				"-rename"));
	}

	private final List<File> renamed = new ArrayList<File>();

	private String appendUniqueSuffix(File inDirectory, String newName) {
		int i = 0;
		String suffix = "";
		while (new File(inDirectory, newName + suffix).exists()
				|| renamed.contains(new File(inDirectory, newName + suffix))) {
			suffix = "_" + i;
			i++;
		}
		newName += suffix;
		return newName;
	}

	private void execute(File parentDirectory, boolean rename) throws Exception {
		for (File file : parentDirectory.listFiles()) {
			if (file.isDirectory()) {
				renameDirectory(file, rename);
			}
		}
	}

	private File findContainedJpg(File inDirectory) {
		File[] sub = inDirectory.listFiles();
		for (File s : sub) {
			if (s.getName().toLowerCase().endsWith(".jpg")
					|| s.getName().toLowerCase().endsWith(".jpeg")) {
				return s;
			}
		}
		return null;
	}

	private String getPhotoCreated(File jpg) throws Exception {
		Process proc = Runtime.getRuntime().exec(
				"exiftool.exe -common \"" + jpg.getPath() + "\"");
		InputStream in = proc.getInputStream();
		StringBuilder result = new StringBuilder();
		byte[] buf = new byte[256];
		int r = 0;
		while ((r = in.read(buf)) != -1) {
			result.append(new String(buf, 0, r));
		}
		Matcher m = Pattern.compile(
				"Date/Time Original              : (.*) (.*)").matcher(
				result.toString());
		if (m.find()) {
			return m.group(1).replaceAll("\\:", "-");
		}
		return null;
	}

	private void renameDirectory(File file, boolean rename) throws Exception {
		File fileWithDate = findContainedJpg(file);
		if (fileWithDate == null) {
			System.err.println("no jpg file found in " + file.getPath());
			return;
		}
		String newName = getPhotoCreated(fileWithDate);
		if (newName == null) {
			System.err.println("exif missing in first jpg file "
					+ fileWithDate.getPath());
			return;
		}

		newName = appendUniqueSuffix(file.getParentFile(), newName);
		if (rename) {
			System.out.println("renaming " + file.getPath() + " to " + newName);
			file.renameTo(new File(file.getParentFile(), newName));
		} else {
			System.out.println("folder " + file.getPath()
					+ " would be renamed to " + newName);
			renamed.add(new File(file.getParentFile(), newName));
		}
	}
}

and locate it in the same folder as the exiftool. Then call
java RenameFoldersByPhotoCreatedDate <location_of_DCIM_folders>
and then if the output looks right
java RenameFoldersByPhotoCreatedDate <location_of_DCIM_folders> -rename
to actually perform the renaming of the folder.


Feedback


feedback chooser