Zum Inhalt springen

exif2foldername - Change the folder name

This little script is very similar to exif2folder, which for my purposes, in the end, didn't suit the purpose initially intended. The difference here is that this little script will not change the mtime of a folder, but will actually change the folder name. It looks again for the oldest image (photo with exif information) in all subfolders of a given start folder and prepends the oldest image date found in earch folder to that of the folder's name.

What's the practical purpose of this?

In Windows sorting can be done by name, date etc. However, with standard settings, it uses the mtime of the folder, e.g. when the folder was last modified. The problem is, when you insert a new image into a folder oder even just open the folder, the mtime of the folder is changed. So, sorting here in not according to when the folder was created. When we are out geocaching, we usually make photos of each cache found and later place it in a folder with the GC number of that cache. Now, if needed, I can very quickly locate when I acutally found that cache, just by looking at the folder date and searching for it via the GC number.

The output

exif2foldername Output

The script code

You will need PHP installed your system for this to work. I've written the script for my Ubuntu homeserver in mind, but with a little modification it should run on any system with php installed.

## execute with "sudo php /path/to/script/exif2foldername.php" ##
$rootpath "/mnt/raid/fotos/Geocaching"//path to your main fotos folder (must have subfolders)

function get_oldest_jpg($directory) { 
  if (
$handle opendir($directory)) { 
    while (
false !== ($file readdir($handle))) { 
$files[] = $file
    foreach (
$files as $val) { 
$extension substr($valstrrpos($val'.'));
      if (
is_file($directory.$val) && ($extension == ".JPG" || $extension == ".jpg")) { 
$file_date[$val] = filemtime($directory.$val); 

$oldest key($file_date); 

"Folder renamer according to oldest JPEG in folder from exif\n";
"Will only rename folders beginning with the letters \"GC\"\n";

$count $renamecount 0;
$folders glob($rootpath.'/*',GLOB_ONLYDIR);

foreach (
$folders as $folder) {
//get oldest file in folder, (filemtime)
$oldest_jpg get_oldest_jpg("$folder/");
//read exif date from oldest_jpg
$exif_data exif_read_data ($folder."/".$oldest_jpg);
$edate $exif_data['DateTimeOriginal'];
//result from edate = 2010:12:05 20:15:57;
$foldername substr($folderstrrpos($folder'/'));
$prepend_date str_replace(":","-",substr($edate,0,10));
$prepend_time str_replace(":","",substr($edate,11));
$final_date $prepend_date "_" $prepend_time;
$oldfolder substr($foldername,1);
$newfolder $final_date." ".substr($foldername,1);
$newpath $rootpath."/".$newfolder;
//now change the folder date to the exif date of oldest image in that folder
if (substr($oldfolder,0,2) == "GC") {
exec("mv \"{$folder}\" \"{$newpath}\"");
"Renamed $folder -> $newpath\n";
if (
$renamecount != 0) {
"Total folders scanned = $count -> Renamed = $renamecount\n\n";

Important things to know

To prevent the script from renaming already changed folders, it only changes folders that beginn with the letters "GC". Previously renamed folders beginn with a number. Probably, you will need to change this feature to your own preference/use. Also it only looks for the exif information in jpegs.

Kommentiere diesen Artikel

Kommentar (erforderlich) (keine Anzeige) (optional)
Ja, erkenne mich, wenn ich das nächste mal hier bin