odog502 has asked for the wisdom of the Perl Monks concerning the following question:

I have a basic Win32::OLE question. Im just trying to retrieve the values of properties for files using OLE. Specifically I want to retrieve the "Date Picture Taken" value from my JPG files. But to start off I cant even get these basic properties...

#!/usr/local/bin/perl use Win32::OLE('in'); $tp = Win32::OLE->GetObject("C:\\testpic-1.JPG"); print "Readable Attribute Value: $tp->{'Readable'}\n"; print "File Name: $tp->{'FileName'}\n";

When I run this I get the following output:

C:\>test.pl Readable Attribute Value: [object] File Name: [object] C:\

Can someone please tell me how to retrieve file properties and specifically the "Date Picture Taken" property?

Replies are listed 'Best First'.
Re: Getting file attributes/properties using Win32 OLE
by Ollie Jones (Novice) on Jan 04, 2010 at 03:24 UTC
    If you're working with camera-produced JPEG files, you might consider using the Image::MetaData::JPEG module to retrieve the picture's timestamp directly from the JPEG metadata.

      Thanks for the suggestion Ollie. You helped me find the solution to the problem. I didn't use Image::MetaData::JPEG but I did use one similar to it, Image::ExifTool::Exif. Your suggestion got me looking for the right thing though thanks. I downloaded the module from CPAN, then I was able to do what I set out to accomplish. Heres the code I wrote for anyone that is interested:

      #!/usr/local/bin/perl # # PicRenamer.pl # script to rename pics and videos based on the date # in the following format "YYYY-MM-DD_HHmmSS_Event.JPG" # # Written by: odog502 # backup all your files before trying this!!! Im not responsible # for any missuse of this script # $dir = 0; $event; $property; $filetype; print 'Enter location of files to be renamed (ex. C:/test): '; chomp($dir = <STDIN>); print 'Enter the name of event that describes these pictures(no sp +aces): '; chomp($event = <STDIN>); print 'Enter the exif property that contains the date(eg Date/Time + Original): '; chomp($property = <STDIN>); # examples "File Modification Date/Time", "Create Date", "Date/Tim +e Original" print 'Enter the extension of the filetype you want modify(e.g. \' +jpg\'): '; chomp($filetype = <STDIN>); chdir ("$dir"); # # gets appropriate files and sorts them by file name # @files = <*.$filetype>; @files = sort {$a cmp $b} @files; # # Go through each filename and use exif to get its properties. # Loop through each property until you find the one with the date # Rename the file to the date found in the property, add the custom # "event" name to the end. # $i = 0; until ($i > $#files) { print "working on file: $i $files[$i]\n"; open (FILEPROP, "C:\\Image-ExifTool-8.00\\exiftool.pl \"$dir\\ +$files[$i]\" |") or die $!; while (<FILEPROP>){ chomp; if ( $_ =~ /$property\s*: (\d{4}):(\d{2}):(\d{2}) (\d{2}): +(\d{2}):(\d{2})/){ print "Changing to: $1-$2-$3_$4$5$6_$event.$filetype\n +"; rename "$files[$i]", "$1-$2-$3_$4$5$6_$event.$filetype +"; } $j++; } close FILEPROP; $i++; }
Re: Getting file attributes/properties using Win32 OLE
by Anonymous Monk on Jan 03, 2010 at 22:41 UTC

      I already have the GetObject function in my script. Or are you implying that I am using it incorrectly? Despite the link being all in VB as far as I can tell Im using it correctly. My only guess is that Im not specifying the optional object class, but since your link is VB I dont know what the perl syntax would be for this.

      The perl example that I am basing my script off of is what I learned from here List Page File Properties

        GetObject can return anything, it varies from system to system.

        So you're supposed to use some calls (i'm guessing QueryObjectType, see in Win32::OLE and on MSDN ) to find what type of object you have, and then look up the available $obj->Methods and $obj->{Properties} on MSDN.

        On my system I get MSHTMLDispHTMLDocument, which is some kind of .NET object ( DispHTMLDocument ), and it doesn't have a FileName or Readable property.

        Maybe you can try Win32::OLE Type Library Browser