Use of dir, file, ext and count (%d%f%e%c)

Started by mitkofr, June 03, 2020, 10:00:43 AM

Previous topic - Next topic

mitkofr

Hi all,

In this example I've successfully made use of %-c and %le:
exiftool '-testname<$createdate ${make;}.%le' -d '%Y/%B/%Y-%m-%d %H.%M.%S%%-c' some_image.jpg

however, I am unable to reproduce when using the API:

if ($exifTool->GetValue('Make'))
{
   $exifTool->SetNewValuesFromFile($image, 'testname<${createdate#;DateFmt("%Y/%B/%e-%b %H.%M.%S%%-c")} ${make;}.%le');
} else {
   $exifTool->SetNewValuesFromFile($image, 'testname<${createdate#;DateFmt("%Y/%B/%e-%b %H.%M.%S%%-c")}.%le');
}

my $result = $exifTool->WriteInfo($image);


I bet it's obvious but I can't figure it out. My files get renamed with literal '%-c' and "%le" in their names

Phil Harvey

This functionality is not part of the API.  The exiftool application does this in its FilenameSPrintf() function.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).


mitkofr

Btw, does my code below look good/correct. Is it roughly equivalent to:

exiftool -$filename'<$createdate.%le'          -d $source'/%Y/%B/%Y-%m-%d %H.%M.%S%%-c' \
         -$filename'<$createdate ${make;}.%le' -d $source'/%Y/%B/%Y-%m-%d %H.%M.%S%%-c' \
         $source

Is it as efficient?
Maybe I should be checking Image::ExifTool::GetFileType() vs (-f $image)
My code:

   my $filename = $dry ? 'testname' : 'filename';
   my %cdates;

   foreach my $image (glob "'$source/*'")
   {
      if (-f $image)
      {
         # Compare CreateDate and DateTimeOriginal
         my $dates = $exifTool->ImageInfo($image, qw/CreateDate DateTimeOriginal/, {DateFormat => '%d-%b-%Y %Hh%Mm%S'});

         my $cdate = $dates->{CreateDate};
         my $ddate = $dates->{DateTimeOriginal};
         $cdate //= '';
         $ddate //= '';

         if ($cdate ne $ddate)
         {
            warn YELLOW."CreateDate ($cdate) differs from DateTimeOriginal ($ddate)".RESET, "\n";
            # TODO: next;
         }

         # Add -num (%c) before extension for images having the same Createdate
         my $num = '';
         if ($cdate)
         {
            $cdates{$cdate}++;
            $num = '-'.($cdates{$cdate}-1) if $cdates{$cdate} > 1;
         }

         # Sort camera shots
         my $info;
         my ($basename, $dirs, $suffix) = fileparse($image, qr/\.[^.]+$/);

         if ($exifTool->GetValue('Make'))
         {
            $info = $exifTool->SetNewValuesFromFile($image, $filename.'<${createdate#;DateFmt("'.$source.'/%Y/%B/%d-%b-%Y %Hh%Mm%S")} ${make;}'.$num.lc($suffix));
         } else {
            $info = $exifTool->SetNewValuesFromFile($image, $filename.'<${createdate#;DateFmt("'.$source.'/%Y/%B/%d-%b-%Y %Hh%Mm%S")}'.$num.lc($suffix));
         }

         # Errors while sorting
         unless (exists $info->{Warning} or exists $info->{Error})
         {
            my $result = $exifTool->WriteInfo($image);

            # Errors while writing
            unless ($result == 1)
            {
               if ($exifTool->GetValue('Warning'))
               {
                  warn "Warning writing $basename ", YELLOW, $exifTool->GetValue('Warning'), RESET, "\n";
               }
               if ($exifTool->GetValue('Error'))
               {
                  warn "Error writing $basename ", RED, $exifTool->GetValue('Error'), RESET, "\n";
               }
            }
         } else {
            if (exists $info->{Warning})
            {
               warn "Warning moving $basename ", YELLOW, $info->{Warning}, RESET, "\n";
            }
            if (exists $info->{Error})
            {
               warn "Error moving $basename ", RED, $info->{Error}, RESET, "\n";
            }
         }
      }
   }


Phil Harvey

Your code is not as efficient because you are reading the source file twice (once with ImagInfo and once with SetNewValuesFromFile)

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).