2010-06-04 44 views
7

我有一个VBScript,用于检查远程计算机上目录中是否存在文件。我期待检索所述文件的“产品版本”(不是文件版本”),但我似乎无法弄清楚如何在VBScript中做到这一点。如何在VBScript中检索文件的“产品版本”

我目前使用Scripting.FileSystemObject来检查文件的存在。

谢谢。

+0

下面是关于JScript的一个非常类似的问题 - [检测使用JScript中的exe文件的版本和公司名称](HTTP://计算器.com/questions/1674134 /) – Helen 2010-06-07 20:28:21

回答

0

我不认为你可以在vbScript中做到这一点。我可能是错的。

这是作为链接到一个powershell脚本,做你在问什么。

link text

这里是输出为我的Windows目录的模样。

PS脚本:> ls c:\ windows * .exe | 。\ get-fileversion.ps1

ProductVersion FileVersion FileName -------------- ----------- -------- 2.7 .3.0 2.7.3.0 C:\ windows \ agrsmdel.exe

2

您可以使用Shell.Namespace在文件上获得extended properties,其中之一是产品版本。 GetDetailsOf函数应该可以工作。你可以用下面的代码进行测试,以得到一个想法:

Dim fillAttributes(300) 

Set shell = CreateObject("Shell.Application") 
Set folder = shell.Namespace("C:\Windows") 

Set file = folder.ParseName("notepad.exe") 

For i = 0 to 299 
    Wscript.Echo i & vbtab & fillAttributes(i) _ 
     & ": " & folder.GetDetailsOf(file, i) 
Next 

有一点要注意的:

文件的扩展属性的Windows版本之间的不同。因此,产品版本索引号根据您使用的Windows版本而变化。你可以使用上面的代码来确定它们是什么。从我的测试,我认为有如下几点:

  • 视窗XP - 39
  • 的Windows Vista - 252级
  • 的Windows 7 - 268
  • 的Windows 2008 R2 SP1 - 271级
  • 的Windows 2012 R2 - 285

您还可能会发现以下post有帮助。

8

我使用的功能稍微修改了前面的例子。该功能将路径和文件名,并返回在XP和Vista中的“产品版本”

Function GetProductVersion (sFilePath, sProgram) 
Dim objShell, objFolder, objFolderItem, i 
If FSO.FileExists(sFilePath & "\" & sProgram) Then 
    Set objShell = CreateObject("Shell.Application") 
    Set objFolder = objShell.Namespace(sFilePath) 
    Set objFolderItem = objFolder.ParseName(sProgram) 
    Dim arrHeaders(300) 
    For i = 0 To 300 
     arrHeaders(i) = objFolder.GetDetailsOf(objFolder.Items, i) 
     'WScript.Echo i &"- " & arrHeaders(i) & ": " & objFolder.GetDetailsOf(objFolderItem, i) 
     If lcase(arrHeaders(i))= "product version" Then 
      GetProductVersion= objFolder.GetDetailsOf(objFolderItem, i) 
      Exit For 
     End If 
    Next 
End If 
End Function 

我发现,属性的位置偶尔改变(不知道为什么),所以我找了“产品版本“属性,一旦找到它就退出循环。注释掉行会显示所有属性和值,如果可用

0
Wscript.Echo CreateObject("Scripting.FileSystemObject").GetFileVersion("C:\Windows\notepad.exe") 
+0

OP说*产品版本*,不是*文件版本*。它们不是同一件事。 – Helen 2011-09-08 07:07:22

+0

不是正确的答案。 – CCoder 2012-11-12 13:38:38

0
' must explicitly declare all variables 
Option Explicit 
' declare global variables 
Dim aFileFullPath, aDetail 
' set global variables 
aFileFullPath = "C:\Windows\Notepad.exe" 
aDetail = "Product Version" 
' display a message with file location and file detail 
WScript.Echo ("File location: " & vbTab & aFileFullPath & vbNewLine & _ 
aDetail & ": " & vbTab & fGetFileDetail(aFileFullPath, aDetail)) 
' make global variable happy. set them free 
Set aFileFullPath = Nothing 
Set aDetail = Nothing 
' get file detail function. created by Stefan Arhip on 20111026 1000 
Function fGetFileDetail(aFileFullPath, aDetail) 
' declare local variables 
Dim pvShell, pvFileSystemObject, pvFolderName, pvFileName, pvFolder, pvFile, i 
' set object to work with files 
Set pvFileSystemObject = CreateObject("Scripting.FileSystemObject") 
' check if aFileFullPath provided exists 
If pvFileSystemObject.FileExists(aFileFullPath) Then 
' extract only folder & file from aFileFullPath 
pvFolderName = pvFileSystemObject.GetFile(aFileFullPath).ParentFolder 
pvFileName = pvFileSystemObject.GetFile(aFileFullPath).Name 
' set object to work with file details 
Set pvShell = CreateObject("Shell.Application") 
Set pvFolder = pvShell.Namespace(pvFolderName) 
Set pvFile = pvFolder.ParseName(pvFileName) 
' in case detail is not detected... 
fGetFileDetail = "Detail not detected" 
' parse 400 details for given file 
For i = 0 To 399 
' if desired detail name is found, set function result to detail value 
If uCase(pvFolder.GetDetailsOf(pvFolder.Items, i)) = uCase(aDetail) Then 
fGetFileDetail = pvFolder.GetDetailsOf(pvFile, i) 
End If 
Next 
' if aFileFullPath provided do not exists 
Else 
fGetFileDetail = "File not found" 
End If 
' make local variable happy. set them free 
Set pvShell = Nothing 
Set pvFileSystemObject = Nothing 
Set pvFolderName = Nothing 
Set pvFileName = Nothing 
Set pvFolder = Nothing 
Set pvFile = Nothing 
Set i = Nothing 
End Function 
+1

欢迎来到SO。请在您的答案中解释代码。 – Tim 2012-11-08 09:27:59