User Tools

Site Tools


software:datetofile

photo sorting - dateTofile

This page describes a solution to help organizing files with different creation dates. It is an alternative to the great exiftool. Practical for merging different series of photos from cameras with different time settings into one directory. By adding the date and time to the leading part of a filename, files can be sorted chronologically. Additionally an offset can be applied to the date and time to correct time differences between cameras. The solution consists of a perl script which adds the date and time to the filename, controlled by an applescript to allow easy drag and drop functionality from the desktop.

To understand the functionality better, take for example the following file:

  • file name: Img002.jpg
  • Creation date: 23rd of Mai, 2012, 10.07.49 PM

After selecting the file, drag and drop it onto the applescript. Depending on the offset, the filename will be modified as follows:

Offset Original file After drag and drop Script name (internal command)
No offset Img002.jpg 20120523_220749.Img002.jpg dateTofile.app ./date2file Img002.jpg
Offset +1 hr Img002.jpg 20120523_230749.Img002.jpg dateTofile+3600.app ./date2file -t +3600 Img002.jpg
Offset -1 day Img002.jpg 20120423_220749.Img002.jpg dateTofile-86400.app ./date2file -t -86400 Img002.jpg

When using the script directly via the terminal, the script can be invoked with the following command:

./date2file [-t offset] file1 file2 ... fileN

where [ ] is optional.

If it is necessary to change the date-time stamp, the script can be run again with the date-time stamped file. Instead of writing an additional date-time stamp, the current one is being replaced instead. This prevents writing double time-stamps like 20120523_230749.20120523_220749.Img002.jpg . For example:

Original file After drag and drop Offset Script name (internal command)
20120523_220749.Img002.jpg 20120523_240749.Img002.jpg +2 hr dateTofile+7200.app ./date2file -t + 7200 20120523_220749.Img002.jpg
20120523_070749.Img002.jpg1)) 20120523_240749.Img002.jpg +2 hr dateTofile+7200.app ./date2file -t + 7200 20120523_070749.Img002.jpg

Download

The scripts can be downloaded here: http://www.auditeon.com/xyz/dateTofile.app.zip. The perl script is already stored in the following directory:

dateTofile.app/Contents/Resources/

Manual installation

  1. Save the applescript, named dateTofile, from below to a directory.
  2. Copy the perl script, named date2file, into the following subdirectory of the applescript directory: dateTofile.app/Contents/Resources/
  3. make the perl script executable with:
    chmod +x date2file

Applescript - dateTofile

-- This droplet processes both files or folders of files dropped onto the applet
 
on open (these_items)
	repeat with i from 1 to the count of these_items
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
			--		else if (alias of the item_info is false) and (the name extension of the item_info is "pdf") then
		else if (alias of the item_info is false) then
			process_item(this_item)
		end if
	end repeat
end open
 
-- this sub-routine processes folders
on process_folder(this_folder)
	set these_items to list folder this_folder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as text) & (item i of these_items))
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
			--		else if (alias of the item_info is false) and (the name extension of the item_info is "pdf") then
		else if (alias of the item_info is false) then
			process_item(this_item)
		end if
	end repeat
end process_folder
 
-- this sub-routine processes files
on process_item(this_item)
	-- NOTE that the variable this_item is a file reference in alias format
	set full_path to the POSIX path of this_item
	try
		set perl_script to quoted form of POSIX path of (path to resource "date2file")
 
		tell application "Finder"
			set x to path to me
			set y to name of file x as text
		end tell
		if ((offset of "+" in y) is 0) and ((offset of "-" in y) is 0) then
			tell application "Terminal"
				do shell script "/usr/bin/perl " & perl_script & space & quoted form of full_path
			end tell
		else
			set t_offset to getNumerals(y)
			tell application "Terminal"
				do shell script "/usr/bin/perl " & perl_script & space & "-t" & space & t_offset & space & quoted form of full_path
			end tell
		end if
	on error the error_message number the error_number
		display dialog "Error: " & the error_number & ". " & the error_message
	end try
end process_item
 
 
on getFileName(thefile)
	set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {":"}}
	set mytextfilename to last text item of (thefile as text)
	set AppleScript's text item delimiters to oldDelims
	return mytextfilename
end getFileName
 
on getNumerals(input)
	set digits to "-+1234567890"
	copy input to nonNumbers
	repeat with thisnumber in digits
		set AppleScript's text item delimiters to thisnumber
		set nonNumbers to every text item of nonNumbers as text
	end repeat
	try
		repeat with thischar in nonNumbers
			set endTest to item 1 of nonNumbers
			set AppleScript's text item delimiters to endTest
			set input to every text item of input
			set nonNumbers to every text item of nonNumbers
			set AppleScript's text item delimiters to ""
			set input to input as text
			set nonNumbers to nonNumbers as text
		end repeat
	end try
	return input
end getNumerals

Perl script - date2file

The script below will add a leading date-time stamp to a filename.

#!/usr/bin/perl
use File::Basename;
use File::Spec;
use Time::Piece;
use File::Copy;
use strict;
 
if (!exists $ARGV[0]) {
 print "$0"." v0.1\nAdd/correct a filename with a leading date and time taken from the file creation date.\nIf a file contains already a leading date/time, then the time will be overwritten. This is useful to correct for an offset afterwards.\nIf trailing part of a filename only contains the date, the script will add the time.\n\nUsage: [-t offset] file1 file2 file3 ... fileN\n[] is optional, offset is a signed value in hours.\n\n example: date2time -t -2 foto1.jpg foto2.jpg\n\nExample:\nA file foto1.jpg which was created on 25 Feb. 2012 on 11.00 pm will be named to 20120225_230000.foto1.jpg\n";
 exit;
}
 
my $file_time;
my $fdate;
my $ftime;
my $offset;
my $file_dirname;
my $file_basename;
my $file_target; # this string will have the composited final name
my $file_datepart; # will be used to analyze whether file has already the date-time pattern
my $file_timepart; # see above
 
if ($ARGV[0] eq '-t') {
  shift @ARGV;
  if ($ARGV[0] =~ /^[+-]?\d+$/ ) {
    $offset=$ARGV[0];
    shift @ARGV;
  } else {
    print "Missing time offset\n";
    exit;
  }
}
 
foreach (@ARGV) {
  $file_dirname = dirname($_);
  if ($file_dirname eq '.') { # empty directories have a dot. This should be removed!
    $file_dirname = '';
  }
  $file_basename = basename($_);
  if (length($file_basename) >= 15) { # check if filename has already date-time pattern
    $file_datepart = substr($file_basename, 0,8);
    $file_timepart = substr($file_basename, 9,6);
    if ($file_datepart =~ /^[+-]?\d+$/ ) {
      if ($file_timepart =~ /^[+-]?\d+$/ ) {
        if (substr($file_basename, 8,1) eq '_') {
          if (substr($file_basename,15,1) eq '.') {
            # is already in date format;
            $file_basename = substr($file_basename,16);
          }
        }
      }
    }
  }
  $file_time = $offset + localtime((stat $_)[9]); # 9th element contains last modify time
  $fdate = localtime($file_time)->ymd(''); #yyyymmdd format
  $ftime = localtime($file_time)->hms(''); #hhmmss 24 hours format
  $file_target = File::Spec->catfile($file_dirname, $fdate."_".$ftime.".".$file_basename);
  move($_,$file_target);
 
}
1)
(Please note that for calculating the new offset to the leading date-time stamp, still the original file creation date is being used, instead of the current leading date-time stamp.
software/datetofile.txt · Last modified: 2013/02/09 21:34 by admin