Author Topic: Use of dir, file, ext and count (%d%f%e%c)  (Read 381 times)

mitkofr

  • Newbie
  • *
  • Posts: 5
Use of dir, file, ext and count (%d%f%e%c)
« on: June 03, 2020, 10:00:43 AM »
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:
Code: [Select]
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

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 17044
    • ExifTool Home Page
Re: Use of dir, file, ext and count (%d%f%e%c)
« Reply #1 on: June 03, 2020, 10:39:48 AM »
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, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

mitkofr

  • Newbie
  • *
  • Posts: 5
Re: Use of dir, file, ext and count (%d%f%e%c)
« Reply #2 on: June 03, 2020, 10:52:26 AM »
Thanks

mitkofr

  • Newbie
  • *
  • Posts: 5
Re: Use of dir, file, ext and count (%d%f%e%c)
« Reply #3 on: June 08, 2020, 09:13:09 AM »
Btw, does my code below look good/correct. Is it roughly equivalent to:
Code: [Select]
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:
Code: [Select]
   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

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 17044
    • ExifTool Home Page
Re: Use of dir, file, ext and count (%d%f%e%c)
« Reply #4 on: July 13, 2020, 08:24:16 PM »
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, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).