How to truncate a filename while renaming?

Started by Archive, May 12, 2010, 08:53:54 AM

Previous topic - Next topic

Archive

[Originally posted by metanewbie on 2006-06-06 03:08:28-07]

Hello,

It's been a while since I've been on a nix platform and using perl, and when I was, I wasn't an expert by any means.  Please bear with me.

1st, I would like to copy the EXIF date to the file's Created date then rename the file using this date, prefixed by my initials, suffixed by the number associated with the initial file (truncate the IMG) and make sure the extension is in upper case.  And lastly copy the file(s) to another directory (or just do this when renaming).

I'm reading the documention, but any help would be appreciated.  So far, I've been able to rename the file with the date (pulling it from the EXIF - but I'd also like to copy it to the Files Create date - which remains blank).  I also suffixed the date with the filename, but I haven't figured out how to truncate it.  Also no luck on figuring out how to make sure the extension is capitalized.

Any help would be greatly appreciated.

Thanks a lot

Archive

[Originally posted by metanewbie on 2006-06-06 03:56:46-07]

I tried this to copy the files exif date to the files create date:

Code:
exiftool "-DateTimeOriginal>FileCreatedDate" filename.cr2

and then this:

exiftool "-DateTimeOriginal>FileCreateDate" filename.cr2

No go, the files were unchanged...

Archive

[Originally posted by metanewbie on 2006-06-06 04:55:50-07]

Made another try with copying the exif date to create date, this time I did
Code:
exiftool '-DateTimeOriginal>CreateDate' filename.cr2
and it at least completed the process (changed the file), but the CreateDate is today, and not the EXIF date as I'd hoped.

Archive

[Originally posted by exiftool on 2006-06-06 11:29:47-07]

Currently, this sort of control in manipulating filenames is not possible with the 'exiftool' script.  I am currently working on enhancing this feature, but it may not be possible to add this sort of control without over complicating the already confusing command-line interface.

Your command to set "FileCreatedDate" or "FileCreatedDate" didn't work because there is no such tag.  Use "exiftool -s" to see the names of all tags in the image -- these are the ones you can write.

If you are still having trouble setting the CreateDate, send me a terminal transcript.  Below is an example from my terminal:

Code:
> exiftool a.cr2 -a -datetimeoriginal -createdate
Date/Time Original              : 2005:08:03 18:59:18
Create Date                     : 2005:08:03 18:59:18
> exiftool a.cr2 "-createdate=2006:01:01 00:00:00"
    1 image files updated
> exiftool a.cr2 -a -datetimeoriginal -createdate
Date/Time Original              : 2005:08:03 18:59:18
Create Date                     : 2006:01:01 00:00:00
> exiftool a.cr2 "-datetimeoriginal>createdate"
    1 image files updated
> exiftool a.cr2 -a -datetimeoriginal -createdate
Date/Time Original              : 2005:08:03 18:59:18
Create Date                     : 2005:08:03 18:59:18

Archive

[Originally posted by metanewbie on 2006-06-06 18:05:55-07]

Thanks a lot 'exiftool'!  It was the file system's create date I wanted to change, not those two.  I guess I can't write it.  When doing what you did above, I've got no problems.  But I remain to see the improper create date when viewing in OSX's finder.  Not a big deal.

OFF TOPIC:  I'm still looking how to truncate and capitalize a name easily in the shell.  For example, if I have xxximg1234.dng - how can I change it to XXX1234.DNG ?

Archive

[Originally posted by exiftool on 2006-06-06 19:03:16-07]

Sorry, I didn't realize you were talking about the filesystem create date.  Both the create date and the modify date are set via the FileModifyDate tag.

I had a bit more time this afternoon, so here is a Perl script that will do what you originally asked.  It is fairly basic, only operates on a single file at a time, and inserts my initials instead of yours, but it should give you an idea of how it could be done.

Code:
#!/usr/bin/perl -w
use strict;
BEGIN { push @INC, 'lib' }
use Image::ExifTool;

my $infile = shift;
my $outdir = shift;

$outdir and -d $outdir or die "Must specify output directory\n";
-e $infile or die "$infile does not exist";

my $exifTool = new Image::ExifTool;
my $info = $exifTool->ImageInfo($infile);
my $date = $info->{DateTimeOriginal} or die "No DateTimeOriginal\n";

$exifTool->SetNewValue(FileModifyDate => $date);

# change the colons in the date to hyphens
$date =~ tr/:/-/;

# remove the time
$date =~ s/ .*//;

# generate output file name from the input
my $outfile = uc($infile);

# remove leading DIR/IMG_ and substitute the
# new directory, my initials and date
$outfile =~ s{.*IMG_}{$outdir/PH_${date}_};

-e $outfile and die "$outfile already exists\n";

# rewrite the file, setting the filesystem time at the same time
$exifTool->WriteInfo($infile, $outfile);

-e $outfile or die "Error creating $outfile\n";

unlink $infile;     # erase original

print "Done\n";

# end

- Phil

Archive

[Originally posted by metanewbie on 2006-06-07 04:46:10-07]

Wow Phil, thanks a lot.  I think I follow most (maybe some) of what is going on.  How do those expressions where you are changing the colons in the date to hyphens and removing the time work?  The final filesystem info looks just as I want it now, but the filename is appearing with hyphens in the date, (2006-06-06) and I just want it to show as GGG20060606_3981.DNG.  So I tried removing the hyphen in the area where you are replacing colons with hyphens, but that just resulted in the filename having slashes.  The extension does get capitalized in your script, just as I wanted it...where is that in the script?  When running it, why can't I put *.dng (for example) to run it on multiple files?  (When trying this it just ran it on the first file).  Can I change the line
Code:
my $infile = shift;
 to  
Code:
my $infile = "*.*";
 ?

Thanks so much for helping.

PS:  I'm trying to incorporate all this into a RAW file script I'm creating.  I am more comfortable (not saying much though) in shell scripting than Perl.  Can I assume that Perl is capable of doing everything that shell scripting is capable of?

Archive

[Originally posted by metanewbie on 2006-06-07 05:36:44-07]

Hi Phil,

Well, I ended up just using a loop to repeat the script until all the dngs were done.  It's ok, I'm sure there is a more elegant way.  Would still like to get rid of the hyphens in the filename though...i'll be mucking around with it a bit more tonight...thanks again

Guhan

Archive

[Originally posted by exiftool on 2006-06-07 11:01:52-07]

Somehow I knew I'd get into maintaining this script if I wrote it for you... hehe https://exiftool.org/forum/Smileys/default/wink.gif" alt="Wink" border="0" />  No problem though.

Below is a new version of the script that will allow wildcards in the file name.  ie) "scriptname srcdir/*.dng dstdir"

I have also added a couple of comments to answer your questions.

Code:
#!/usr/bin/perl -w
use strict;
BEGIN { push @INC, 'lib' }
use Image::ExifTool;

my $outdir = pop;
$outdir and -d $outdir or die "Must specify output directory\n";

my $exifTool = new Image::ExifTool;
my $count = 0;

for (;;) {

    my $infile = shift;
    last unless defined $infile;

    -e $infile or die "$infile does not exist";
   
    my $info = $exifTool->ImageInfo($infile);
    my $date = $info->{DateTimeOriginal};
    unless ($date) {
        warn "No DateTimeOriginal for $infile\n";
        next;
    }
   
    $exifTool->SetNewValue(FileModifyDate => $date);
   
    # remove colons in date
    $date =~ s/://g;
   
    # remove the time
    $date =~ s/ .*//;
   
    # generate output file name from the input
    # (and convert to uppercase)
    my $outfile = uc($infile);
   
    # remove leading DIR/IMG_ and substitute the
    # new directory, my initials and date
    $outfile =~ s{.*IMG_}{$outdir/GGG${date}_} or next;
   
    if (-e $outfile) {
        warn "$outfile already exists\n";
        next;
    }
   
    # rewrite the file, setting the filesystem time at the same time
    $exifTool->WriteInfo($infile, $outfile);
   
    -e $outfile or die "Error creating $outfile\n";
   
    unlink $infile;     # erase original
    ++$count;
}
print "Processed $count files\n";

# end

Archive

[Originally posted by metanewbie on 2006-06-07 18:39:20-07]

Thanks a lot Phil, that is awesome.  I hope you can use it too, I know I'll be getting a lot of use out of this.  I think I need to learn more Perl.

If you're ever in the Toronto area, give me an email, I'll take you out for a beer https://exiftool.org/forum/Smileys/default/smiley.gif" alt="Smiley" border="0" />