我有一些Word文档,其创建日期(doc
和docx
)都不同。我想重命名文件以包含此创建日期。这可能吗?这不是文件系统的最后修改日期,这是内部Word创建日期,可在File->Info->Properties
Word内部查看
例如,我想将文档重命名Summary meeting 123.doc
为123 2010-01-01 Summary meeting 123
。123部分可以通过简单的正则表达式来实现,但是如何获取文件创建日期呢?
另一个解决方案是将文件系统的最后修改日期设置为内部单词创建日期。
使用此PowerShell脚本:(位于此处:http : //blogs.technet.com/b/heyscriptingguy/archive/2012/08/02/use-powershell-to-find-specific-word-built-in-properties。 aspx)
Edit2:编辑了脚本以匹配您的特定方案。
Param(
$path = "Q:\Test",
[array]$include = @("*.docx","*.docx")
)
$application = New-Object -ComObject word.application
$application.Visible = $false
$binding = "System.Reflection.BindingFlags" -as [type]
[ref]$SaveOption = "microsoft.office.interop.word.WdSaveOptions" -as [type]
## Get documents
$docs = Get-childitem -path $Path -Recurse -Include $include #Remove -Recurse if you dont want to include subfolders.
## Iterate documents
foreach($doc in $docs)
{
try
{
## Get document properties:
$document = $application.documents.open($doc.fullname)
$BuiltinProperties = $document.BuiltInDocumentProperties
$pn = [System.__ComObject].invokemember("item",$binding::GetProperty,$null,$BuiltinProperties,"Creation Date")
$value = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$pn,$null)
## Clean up
$document.close([ref]$saveOption::wdDoNotSaveChanges)
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($BuiltinProperties) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($document) | Out-Null
Remove-Variable -Name document, BuiltinProperties
## Rename document:
if($doc.PSChildName -match "^(.+)(\d{3})(.+)$") # Matches "(Summary meeting )(123)(.doc)"
{
$date=$value.ToString('yyyy-MM-dd');
$newName = "$($matches[2]) $($date) $($matches[1])$($matches[2])$($matches[3])";
write-host $newName;
Rename-Item $doc $newName
}
}
catch
{
write-host "Rename failed."
$_
}
}
## Clean up
$application.quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($application) | Out-Null
Remove-Variable -Name application
[gc]::collect()
[gc]::WaitForPendingFinalizers()
将$ path-variable更改为文档所在的文件夹。
使用您需要的任何其他逻辑来编辑#Rename部分,我添加的正则表达式与您在帖子中提供的特定示例匹配,对其进行调整以使其与您的所有文档匹配。可以使用“ matches []”数组(从索引1开始)来引用(括号)内的任何模式。
我建议注释掉“ Rename-Item”行,直到您确定“ $ newName”是正确的。在“ $ newName =”行中的变量周围添加了额外的$(),以便变量将扩展。
PS: 确保启用powershell脚本运行,以管理员身份打开PS,然后输入:“ Set-ExecutionPolicy RemoteSigned”)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句