This is an old revision of the document!
This page describes a perl- and an Applescript which work together to add the (file creation) date and time to the leading part of a filename. This can be helpful when merging photos from different cameras into one directory. An offset can be set to adjust for time differences between cameras.
The applescript works just as a front end to allow for easy drag and drop.
Adding a timestamp in front of the file is done with the perl script.
Example 1: Drag and drop the file Img040.jpg onto dateTofile.app (The file Img040 has been created on 11.40.23 on the 23 of March 2012) The file will be renamed to 20120323_114023.Img040.jpg
Example 2: Rename dateTofile.app to dateTofile+3600.app, drag and drop the file Img040.jpg onto it . The file will be renamed one hour later, to 20120323_124023.Img040.jpg
Example 3: Via the terminal, the script can be invoked with ./date2file [-t offset] file1 file2 … fileN, where [ ] is optional.
If a date-time stamp is incorrect, you can run the script again with the date-time stamped file. Instead of writing an additional date-time stamp, the current one is being replaced instead.
You can download the script here: http://www.auditeon.com/xyz/dateTofile.app.zip
-- 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
#!/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; } } # open (MYFILE, '>>/Users/iudex/data.txt'); # print MYFILE "Offset in seconds: $offset"."\n"; 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); # print "original: $_"."\n"; # print $file_target."\n"; # print $file_dirname."\n"; move($_,$file_target); } # close (MYFILE);