RSXS
RSXS

Reputation: 23

How can I replace date modified with date last saved?

I'm trying to organize a large number of .doc and .docx files, but it appears that their "Date modified" and "Date created" metadata are incorrect — probably it was all lost during a move a while back. The "Date last saved" and "Content created" metadata appear to be correct, however, so I'm wondering: is it possible to use Powershell to replace the "Date modified" and "Date created" fields with the information from the "Date last saved" and "Content created" fields?

Upvotes: 2

Views: 3271

Answers (1)

Marc
Marc

Reputation: 492

CreationTime and LastWriteTime are filesystem properties, you can get and set them using get-itemproperty and set-itemproperty.

Creation date and Last save time are word-specific properties. The Scripting Guy tells you how to read them. Once you read them, set them with set-itemproperty.

Here's an example on how to read the two word properties and write them to the filesystem properties for all *.doc and *.docx files in the current directory.

$includeExtensions = @(".doc", ".docx") 
$path = "."
$docs = Get-ChildItem -Path $path | ?{$includeExtensions -contains $_.Extension}

foreach($doc in $docs) {
    $application = New-Object -ComObject word.application
    $application.Visible = $false
    $document = $application.documents.open($doc.FullName)
    $binding = "System.Reflection.BindingFlags" -as [type]
    $properties = $document.BuiltInDocumentProperties

    $lastsavetime = $null
    $creationdate = $null

    foreach($property in $properties)
    {
     $pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null)
      trap [system.exception]
       {
        continue
       }
       if($pn -eq "Last save time") {
            $lastsavetime = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
       } elseif ($pn -eq "Creation date") {
            $creationdate = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
       }                
    }
    
    $document.Close()
    $application.quit()

    "Setting " + $doc.FullName
    Set-ItemProperty $doc.FullName -Name "Creationtime" -Value $creationdate 
    Set-ItemProperty $doc.FullName -Name "LastWriteTime" -Value $lastsavetime 

}

Upvotes: 0

Related Questions