8

I'm developing a Desktop Application with NWJS and I need to get the file properties of an .exe File.

I've tried using the npm properties module https://github.com/gagle/node-properties, but I get an empty Object.

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

            console.log(obj);
        });

I need to get the "File version" Property:

File Properties

I've also tried using fs.stats and no luck. Any ideas?

Gabriel Matusevich
  • 3,835
  • 10
  • 39
  • 58
  • > there is an another way already [https://stackoverflow.com/a/69984615/4348530](https://stackoverflow.com/a/69984615/4348530) – Michael Mao Nov 16 '21 at 06:32

2 Answers2

3

Unless you want to write some native C module, there is hacky way to get this done easily: using windows wmic command. This is the command to get version (found by googling):

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

so you can just run this command in node to get the job done:

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
 }
});
hassansin
  • 16,918
  • 3
  • 43
  • 49
3

If you want the properties provided as an object, you can use get-file-properties. It uses wmic under the hood, but takes care of parsing the output into an easy to use typed object for your application's consumption.

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)
}

Disclaimer: I am the author of get-file-properties

Jack Barry
  • 191
  • 3
  • 7
  • 1
    Worked for me in node js. Can this Run in Linux? – Nyagaka Enock May 31 '21 at 09:51
  • @NyagakaEnock As far as I know this isn't needed in Linux, since Node's native fs.stat() will get any file properties that are available in Linux. I could be wrong on that, but I've dug around and haven't found any real clear equivalent to the data that WMIC.exe pulls for the file in Windows, or mdls on macOS. I'm hoping to add cross-platform support for the get-file-properties module, just haven't had time yet. – Jack Barry Jun 05 '21 at 20:27