Node JS-读取文件属性

问题描述 投票:5回答:2

我正在使用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);
        });

我需要获取“文件版本”属性:

“文件属性”

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

node.js node-webkit file-properties
2个回答
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
 }
});

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的作者>

© www.soinside.com 2019 - 2024. All rights reserved.