如何用上次保存的日期替换修改日期? [英] How can I replace date modified with date last saved?

查看:183
本文介绍了如何用上次保存的日期替换修改日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试组织大量 .doc 和 .docx 文件,但它们的修改日期"和创建日期"元数据似乎不正确 - 可能它们在不久前的移动过程中全部丢失了.但是,上次保存日期"和创建内容"元数据似乎是正确的,所以我想知道:是否可以使用 Powershell 将修改日期"和创建日期"字段替换为上次保存日期"和内容创建"字段?

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?

推荐答案

CreationTimeLastWriteTime 是文件系统属性,您可以使用 get- 获取和设置它们itempropertyset-itemproperty.

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

Creation dateLast save time 是特定于单词的属性.脚本专家 告诉你如何阅读它们.阅读它们后,使用 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.

这里有一个示例,说明如何读取两个单词属性并将它们写入当前目录中所有 *.doc*.docx 文件的文件系统属性.

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 

}

这篇关于如何用上次保存的日期替换修改日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆