Setting the file properties on MS word docuemnts or other 'Structured Storage' documents seems to be a task that several folks have been needing to do, but have not found a clean way to get it done. I found a COM object on the MS support site called dsofile that will access the properties. So using the WIN32::OLE we get a Perl interface to Structured Storage.
################################# # # SetTitleProp.pl # # A quick and dirty script to set the 'title' property of a Structured + Storage file. # The files can be either on an NTFS partition, or could be MS Office +documents on # NTFS or FAT. # # To use it you must have installed the dsofile utility from the micro +soft site. Currently the # link to it is: # # http://support.microsoft.com/default.aspx?scid=kb;EN-US;q224351 # # But if the url is invalid then search for dsofile.exe, or knowledge +article 22435. # # ################################## use strict; use Win32::OLE; use File::Spec; my $PropertyReader = Win32::OLE->new('DSOleFile.PropertyReader', 'Quit +'); my $directory = "g:\\fileproptest"; opendir(DNAME, $directory) || die "Unable to open the requested direct +ory: $directory\n"; while( my $filename = readdir( DNAME ) ) { next if ($filename eq '.' or $filename eq '..'); my $fullfilename = File::Spec->catdir($directory,$filename); my $properties = $PropertyReader->GetDocumentProperties($fullfilen +ame) || die("Unable read file properties for '$fullfilename' ", Win +32::OLE->LastError()); if ( !$properties->{title} || length($properties->{title}) == 0) { print "File '$filename' --- Title not set setting to '$filenam +e'\n"; $properties->SetProperty('title', $filename); } else { print "File '$filename' --- Title property is set to '" . $pr +operties->{title} ."'\n"; } } closedir(DNAME);

Replies are listed 'Best First'.
Re: SetFileProperties
by ironpaw (Novice) on Jan 16, 2003 at 00:32 UTC
    Where do these comments go I don't seem to see them anywhere? This is a great bit of code and it works for viewing doc props but not for setting them. I have tried and tried and played within my abilities. I have posted to discussions all over the show. I am trying to get any property to get written anyone help? I want to reset all our docs to the current company name and also recored and reset the last access and creation dates (that get changed when you edit something). Perl is the source. Any help appreciated
      This code worked for me
      use strict; use Win32::OLE; my $PR = Win32::OLE->new('DSOleFile.PropertyReader', 'Quit'); my $filename = "c:\\test.doc"; my $prop = $PR->GetDocumentProperties($filename) || die("Unable read file properties for '$filename' ", Win32:: +OLE->LastError()); $prop->SetProperty('title', 'My Title'); $prop->SetProperty('subject', 'My Subject'); $prop->SetProperty('author', 'My Author'); $prop->SetProperty('manager', 'My Manager'); $prop->SetProperty('company', 'My Company'); $prop->SetProperty('category', 'My Category'); $prop->SetProperty('keywords', 'My Keywords'); $prop->SetProperty('comments', 'My Comments');
      I couldn't find a way to change the created date though
      poj