Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Decoding OLE::Variant

by blackadder (Hermit)
on Oct 31, 2005 at 10:50 UTC ( [id://504224] : perlquestion . print w/replies, xml ) Need Help??

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

Fellow Monks

Got the green light to convert everything (i.e. scripts) that I can and is possible into Perl :-)

The first batch I have started with are mainly FSO/Wsh based scripts. Below is my code:
#! c:/perl/bin/perl.exe -slw $|++; AUTOLOAD; require 5.008; use strict; use warnings 'all'; use Win32; use Win32::OLE; my $fso = Win32::OLE->new( 'Scripting.FileSystemObject' ); Win32::OLE->Option( Warn => 0 ); my $file = $fso->GetFile("c:\\perl\\bin\\perl.exe"); for my $key (keys %{$file}) { print "$key : $file->{$key}\n"; }
And this was the output I got back:
C:\Perl\FSO>archive.pl Path : C:\Perl\bin\perl.exe Name : perl.exe ShortPath : C:\perl\bin\perl.exe ShortName : perl.exe Drive : Win32::OLE=HASH(0x1a4a3d0) ParentFolder : Win32::OLE=HASH(0x1a4a3ac) Attributes : 32 DateCreated : Win32::OLE::Variant=SCALAR(0x1a4a3dc) DateLastModified : Win32::OLE::Variant=SCALAR(0x1a4a358) DateLastAccessed : Win32::OLE::Variant=SCALAR(0x1a4a40c) Size : 41033 Type : Application C:\Perl\FSO>
What I am after are the date attributes, however I wondering is there away I can decode those variant (i.e. print out the human readble dates) on the same line or on the fly as per say?

Thanks alot.

########### UPDate #########

I just added this
use Win32::OLE::Variant;
And got the human readable date format. Thanks for listening

########### Update 2 ##########

sorry chaps, still have some issues

This is what I changed the code to:
#! c:/perl/bin/perl.exe -slw $|++; AUTOLOAD; require 5.008; use strict; use warnings 'all'; use Win32; use Win32::OLE; use Win32::OLE::Variant; my $fso = Win32::OLE->new( 'Scripting.FileSystemObject' ); Win32::OLE->Option( Warn => 0 ); my $file = $fso->GetFile("c:\\perl\\bin\\perl.exe"); for my $key (keys %{$file}) { if ( $key eq 'Drive') { for (keys %{$key->{'Drive'}}) { print "$_ \n"; } } elsif ($key eq 'ParentFolder') { for (keys %{$key->{ParentFolder}}) { print "$_ \n"; } } else { print "$key : $file->{$key}\n"; } }
and this is the error I got back:
C:\Perl\FSO>archive.pl Path : C:\Perl\bin\perl.exe Name : perl.exe ShortPath : C:\perl\bin\perl.exe ShortName : perl.exe Can't use string ("Drive") as a HASH ref while "strict refs" in use at + C:\Perl\F SO\archive.pl line 22. C:\Perl\FSO>
So I am having problems decoding and OLE hash!

Can I get some enlightment here please on how can decod, on the fly again if poss, this OLE hash?

Thanks for the Help

Blackadder

Replies are listed 'Best First'.
Re: Decoding OLE::Variant
by szbalint (Friar) on Oct 31, 2005 at 11:30 UTC
    Your code:

    for (keys %{$key->{'Drive'}})

    $key is just a scalar here, not a reference, so one way to fix it is writing:

    for (keys %{$file->{$key}})
    or:
    for (keys %{$file->{'Drive'}})

    The same kind of modification can be applied for the ParentFolder key.

    Note: I cannot test the code, so I might be very well wrong.
Re: Decoding OLE::Variant
by meetraz (Hermit) on Oct 31, 2005 at 22:02 UTC
    The problem is that Drive and ParentFolder are themselves objects, with references to other objects, so you have to decide how many levels you want to go down.. Here's an example of going one more level down, but note you'll still get some hash messages in your output, because the drive object references a folder object, and each folder object references a drive, other folders, other files, etc.


    use strict; use warnings 'all'; use Win32; use Win32::OLE; use Win32::OLE::Variant; my $fso = Win32::OLE->new( 'Scripting.FileSystemObject' ); Win32::OLE->Option( Warn => 0 ); my $file = $fso->GetFile("c:\\perl\\bin\\perl.exe"); foreach my $key (keys %$file) { my $value = $file->{$key}; if (ref($value) eq 'Win32::OLE') { foreach my $subkey (keys %$value) { print "$key : $subkey : $value->{$subkey}\n"; } } else { print "$key : $value\n"; } }