PHP

| | Comments (2)

Well after beating my head against the brick wall known as Perl for weeks, I've given up and am going to use PHP. All I want to do is get the datestamp on a file and use it to rename the file to have the datestamp *in* the file. So rename DSC00001.JPG to 2004-04-28-DSC00001.JPG etc. The only way I could find to do this in perl (stat($file)) works perfectly on a *nix system, but not Win32. So I've gone with PHP, which works better when used with a webserver, but I'm sure I'll be able to beat it into submission and get it down to merely running a shortcut on my desktop.

I will also then be able to start figuring out my USA photos in earnest. I haven't wanted to touch the files and resize them until I had the datestamp situation sorted and the files renamed.

2 Comments

ding said:

i believe this is what exif is for

July 15, 2004 12:31 AM

   

Thomas Gemal said:

Something like this should do the job:

# =====================================================================
# Simple Perl tool to add date stamp to file name for JPG files
# (from digital camera).
# May need to change filename criteria for other digital pictures files
#
# Thomas Gemal
# June 1, 2005
# =====================================================================
use strict;

my $filename;
my $filename_new;
my $atime;
my $day;
my $month;
my $year;
my $cnt;

opendir(DIR, ".") or die "can't opendir";
while (defined($filename = readdir(DIR))) {
if ( (-f $filename) && # Must be a file (no directories)
($filename =~ /\.jpg$/i) && # Must end in jpg
($filename =~ /^IMG/i) ) { # Must start with IMG (prevents redo'ing files)

# Get Access Time (seems to be best time stamp for Windows systems)
$atime = (stat($filename))[9];
$day = (localtime($atime))[3];
$month = (localtime($atime))[4] + 1; # Offset by 1
$year = (localtime($atime))[5] + 1900 - 2000; # Offset by 1900. Only interested in last 2 digits

$filename_new = sprintf ("%02d%02d%02d_%s", $year,$month, $day, lc($filename) );
printf ("Renaming: %s %s\n", $filename, $filename_new);
rename ($filename, $filename_new);
$cnt++;
}
}

closedir(DIR);

printf ("Files renamed: %d\n", $cnt);

And if you want to run from Windows Explorer, just create a BAT file with something like:
@perl adddate.pl
@pause

June 2, 2005 2:33 PM

   

Leave a comment

Welcome to my little piece of the Blogosphere!

I am a recent Canberra resident after having spent the first 34 years of my life in Sydney. I am married to my beautiful husband Stu, and we live with several computers and a lot of fish.

Kazza the Blank One home