Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

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

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...