Detecting the version and company name of an exe using jscript

时间秒杀一切 提交于 2019-11-29 08:04:33

Extended file properties, such as company name or product name, can be obtained in scripts using the GetDetailsOf method of the Shell Folder object. The method takes a zero-based index number associated with the extended property and returns the property value as a string. In general, GetDetailsOf can be used to retrieve any type of information that can be displayed in the detailed Shell view (View -> Choose Details). See Retrieving Extended File Properties.

The only problem is that different Windows versions offer different sets of extended file properties. For example, Windows XP has 34 file properties, Windows Vista — 266, Windows 7 — 284. Not only the property indexes can differ, but also the property names (Duration in Windows XP = Length in Windows Vista), which is quite confusing. For a full list of available file properties and their index numbers, see this page or use a script like this one:

var oShell = new ActiveXObject("Shell.Application");
var oFolder = oShell.Namespace("C:");

for (var i = 0; i < 300 /* some large number*/; i++)
  WScript.Echo(i + " " + oFolder.GetDetailsOf(null, i));


Anyway, here's sample code to perform your task on Windows Vista. I couldn't find the Internal Name property (might not have searched properly) so included the File Version and Product Version instead:

var COMPANY_NAME    = 33;
var FILE_VERSION    = 145;
var PRODUCT_NAME    = 251;
var PRODUCT_VERSION = 252;

var oShell  = new ActiveXObject("Shell.Application");
var oFolder = oShell.Namespace("C:\\Windows");
var oFile   = oFolder.ParseName("notepad.exe");

WScript.Echo("Company name: " + oFolder.GetDetailsOf(oFile, COMPANY_NAME));
WScript.Echo("Product name: " + oFolder.GetDetailsOf(oFile, PRODUCT_NAME));
WScript.Echo("File version: " + oFolder.GetDetailsOf(oFile, FILE_VERSION));
WScript.Echo("Product version: " + oFolder.GetDetailsOf(oFile, PRODUCT_VERSION));

Note that you can use GetDetailsOf(null, property_index) to get locale-specific property names (this can be useful on non-English Windows versions):

WScript.Echo(oFolder.GetDetailsOf(null, COMPANY_NAME) + ": " + oFolder.GetDetailsOf(oFile, COMPANY_NAME));

Staying with jscript, by compiling in JScript.NET you could get the info from System.Diagnostics.FileVersionInfo. You could then expose it via COM interop if you need to call it from Windows Scripting Host.

import System.Diagnostics;

private function GetCompanyName(filename) {
    return FileVersionInfo.GetVersionInfo(filename).CompanyName;
 }

 private function GetInternalName(filename) {
    return FileVersionInfo.GetVersionInfo(filename).InternalName;
 }

private function GetProductName(filename) {
    return FileVersionInfo.GetVersionInfo(filename).ProductName;
}

It seems that indexes for GetDetailsOf may differ for the same OS version. For instance, Helen's link reports that "Product name" should be at 270 index for Windows 7, but it's at 267 on my computer. Therefore property indexes are not reliable. Although I didn't do an extensive testing to prove that fact. Since we are targeting only a few languages in our products I've ended up with the following solution:

var wsh = new ActiveXObject("WScript.Shell");
var shell  = new ActiveXObject("Shell.Application");
function GetProductNameIndex()
{
  var folder = shell.Namespace(wsh.ExpandEnvironmentStrings("%WINDIR%"));   // any folder
  for (var i = 0; i < 400; i++)  // some large number of iterations
  {
    var name = folder.GetDetailsOf(null, i);
    // if(!name) break; // this optimization is not working
    if(name.match(/Product name|Название продукта/i)) return i;
  }
  return -1;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!