最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detecting the version and company name of an exe using jscript - Stack Overflow

programmeradmin0浏览0评论

I kwnow how to retrieve the version of an exe using jscript, but I can't find any way to retrieve other info like "Company", "Internal name" or "Product name".

function version_of( file_name ) 
{
   var fso = new ActiveXObject("Scripting.FileSystemObject");
   var f;
   try {
      f = fso.GetFile( file_name )
   } catch( e ) {
      throw new Error( e.number, "Error retrieving version of file ``" + file_name + "'': " + e.description );
   }
   var v = fso.GetFileVersion( f );
   if ( !v ) {
      throw new Error( 1, "File ``" + file_name + "'' has not got a version" );
   }
   return v;
}

WScript.Echo( version_of( "c:\\windows\\system32\\winver.exe" ) );

Maybe I will write my own COM object to do the job...

I kwnow how to retrieve the version of an exe using jscript, but I can't find any way to retrieve other info like "Company", "Internal name" or "Product name".

function version_of( file_name ) 
{
   var fso = new ActiveXObject("Scripting.FileSystemObject");
   var f;
   try {
      f = fso.GetFile( file_name )
   } catch( e ) {
      throw new Error( e.number, "Error retrieving version of file ``" + file_name + "'': " + e.description );
   }
   var v = fso.GetFileVersion( f );
   if ( !v ) {
      throw new Error( 1, "File ``" + file_name + "'' has not got a version" );
   }
   return v;
}

WScript.Echo( version_of( "c:\\windows\\system32\\winver.exe" ) );

Maybe I will write my own COM object to do the job...

Share Improve this question edited Nov 1, 2015 at 0:56 pnuts 59.5k11 gold badges91 silver badges141 bronze badges asked Nov 4, 2009 at 14:36 Alessandro JacopsonAlessandro Jacopson 18.7k16 gold badges104 silver badges156 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Extended file properties, such as pany 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 piling 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 puter. 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;
}
发布评论

评论列表(0)

  1. 暂无评论