2010-06-04 42 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