You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Node.js中获取文件的扩展属性‘keywords’及相关文件系统元数据

提取Windows文件扩展属性(Keywords/Author等)的方法

命令行/PowerShell方式

  • 快速提取单个属性:用PowerShell直接调取系统属性,比如获取作者信息:
    Get-ItemProperty -Path "C:\你的文件路径\example.docx" | Select-Object -Property Author
    
  • 批量查看指定扩展属性:
    $file = Get-Item "C:\你的文件路径\target.file"
    $file.ExtendedProperties | Where-Object { $_.Name -in 'Author', 'Keywords' } | Select-Object Name, Value
    

Python实现(仅Windows)

通过pywin32库调用Windows Shell接口获取属性,先执行pip install pywin32安装依赖:

import win32com.client

def get_file_extended_props(file_path):
    shell = win32com.client.Dispatch("Shell.Application")
    folder_path, file_name = file_path.rsplit('\\', 1)
    folder = shell.Namespace(folder_path)
    file_item = folder.ParseName(file_name)
    
    # 映射属性索引到名称(部分常见属性)
    prop_map = {
        20: 'Author',
        24: 'Keywords',
        15: 'Title',
        19: 'Subject'
    }
    
    result = {}
    for idx, prop_name in prop_map.items():
        prop_value = folder.GetDetailsOf(file_item, idx)
        if prop_value:
            result[prop_name] = prop_value
    return result

# 示例调用
print(get_file_extended_props("C:\\test\\report.pdf"))

注:不同文件类型的属性索引可能略有差异,可遍历0-300的索引值排查所有可用属性。

C#/.NET实现

借助WindowsAPICodePack-Shell库(需通过NuGet安装):

using Microsoft.WindowsAPICodePack.Shell;
using System.Collections.Generic;

public static Dictionary<string, string> GetExtendedFileProperties(string filePath)
{
    var propertyDict = new Dictionary<string, string>();
    
    using (var shellFile = ShellFile.FromFilePath(filePath))
    {
        // 获取作者属性
        var authorProp = shellFile.Properties.GetProperty(SystemProperties.System.Author).ValueAsObject;
        if (authorProp != null)
            propertyDict["Author"] = authorProp.ToString();
        
        // 获取关键词属性
        var keywordProp = shellFile.Properties.GetProperty(SystemProperties.System.Keywords).ValueAsObject;
        if (keywordProp != null)
            propertyDict["Keywords"] = keywordProp.ToString();
    }
    
    return propertyDict;
}

关键说明

Exif数据是嵌入在图片、视频等媒体文件内部的元数据,而Windows的「Keywords」「Author」这类扩展属性是系统或关联应用附加在文件上的属性,存储位置不同,因此常规Exif解析工具无法直接获取这些扩展属性,需要调用Windows系统提供的Shell或文件属性接口来读取。

内容的提问来源于stack exchange,提问作者Vladislav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 14:53:41