Node JS - 读取文件属性

时间:2015-06-30 21:46:55

标签: node.js node-webkit file-properties

我正在使用NWJS开发桌面应用程序,我需要获取.exe文件的文件属性。

我已尝试使用npm属性模块https://github.com/gagle/node-properties,但我得到一个空对象。

properties.parse('./unzipped/File.exe', { path: true }, function (err, obj) {
            if (err) {
                console.log(err);
            }

            console.log(obj);
        });

我需要获得"文件版本"财产:

File Properties

我也试过使用fs.stats而没有运气。 有什么想法吗?

2 个答案:

答案 0 :(得分:3)

除非您想编写一些本机C模块,否则很容易实现这一点:使用windows wmic命令。这是获取版本的命令(通过谷歌搜索找到):

wmic datafile where name='c:\\windows\\system32\\notepad.exe' get Version

所以你可以在节点中运行这个命令来完成工作:

var exec = require('child_process').exec

exec('wmic datafile where name="c:\\\\windows\\\\system32\\\\notepad.exe" get Version', function(err,stdout, stderr){
 if(!err){
   console.log(stdout)// parse this string for version
 }
});

答案 1 :(得分:0)

如果要将属性作为对象提供,则可以使用get-file-properties。它在后台使用wmic,但会负责将输出解析为易于使用的类型化对象,以供应用程序使用。

import { getFileProperties, WmicDataObject } from 'get-file-properties'

async function demo() {
  // Make sure to use double backslashes in your file path
  const metadata: WmicDataObject = await getFileProperties('C:\\path\\to\\file.txt')
  console.log(metadata.Version)
}

免责声明:我是get-file-properties

的作者