我想编辑文件的“注释”字段。
我看到几个如何读取文件元数据的示例,但没有看到如何编辑文件元数据?
从这个论坛帖子来看,您只能 通过 Windows (GUI) shell(COM API)读取此类扩展文档属性Shell.Application- 请参阅下一节。
通过 .NET / PowerShell更新(编辑)属性:
非官方的第 3 方 NuGet 包是 Windows API 的 .NET 包装器,可能会提供解决方案,如此C# 答案中所示。请注意,NuGet 库中存在这些包 () 的许多变体Microsoft.WindowsAPICodePack.*,其中有许多早期看似废弃的包的分支。这个 repo就是一个似乎仍在维护的分叉示例。
具体而言,对于图像文件,确实存在标准 .NET API ,但更新属性需要解决方法:请参阅System.Drawing.Image.GetPropertyItem
否则,您必须使用特定于应用程序的API,例如 Microsoft Office 应用程序的各种 COM 库;例如,New-Object -ComObject Word.Application;本文档可能提供更多信息。
请注意,如果为给定文档类型(例如 Microsoft Word文件)安装了文件资源管理器扩展,则可以通过文件资源管理器中的“属性”对话框以交互方式修改(但不能添加)属性。*.docx
以编程方式读取文档属性:
$file = 'C:\path\to\some\file.doc'
# The index of the property to retrieve.
$propIndex = 24 # Comments
$folder = (New-Object -ComObject Shell.Application).NameSpace((Split-Path $file))
# Output the value of the "Comments" property.
$folder.GetDetailsOf(
$folder.ParseName((Split-Path -Leaf $file)),
24
)
Run Code Online (Sandbox Code Playgroud)
请注意,所有属性值均以字符串形式返回,如果未填写目标属性,则返回空字符串( );''try 0(文件名)作为标准属性来验证调用原则上是否有效。
下面的代码查找所有支持的属性名称的索引:
$folder = (New-Object -ComObject Shell.Application).NameSpace("$pwd")
# Note: Assumes that no indices higher than 1000 exist.
0..1000 | % {
if ($n = $folder.GetDetailsOf($null, $_)) {
[pscustomobject] @{ Index = $_; Name = $n }
}
}
Run Code Online (Sandbox Code Playgroud)
在我的 Windows 10 机器上,结果如下:
$file = 'C:\path\to\some\file.doc'
# The index of the property to retrieve.
$propIndex = 24 # Comments
$folder = (New-Object -ComObject Shell.Application).NameSpace((Split-Path $file))
# Output the value of the "Comments" property.
$folder.GetDetailsOf(
$folder.ParseName((Split-Path -Leaf $file)),
24
)
Run Code Online (Sandbox Code Playgroud)
小智 0
我突然想到,我想你也许可以通过使用 COM 对象来做到这一点
$myFileObj = Get-Item -Path "C:\path\to\file"
$shellCom = New-Object -ComObject Shell.Application
$sDirectory = $shellCom.NameSpace($myFileObj.Directory.FullName)
$sFile = $sDirectory.ParseName($myFileObj.Name)
Run Code Online (Sandbox Code Playgroud)
Get-Member现在对对象执行 a $sFile,因此$sFile | Get-Member. 如果您发现该属性Comments有 setter,您应该能够更改它。它将{get} {set}在Definition. 设置它应该像$sFile.Comments = "blah blah blah"
| 归档时间: |
|
| 查看次数: |
13575 次 |
| 最近记录: |