我有一个VBScript,用于检查远程计算机上目录中是否存在文件。我希望为所述文件检索“产品版本”(不“文件版本”),但我似乎无法想象如何在VBScript中做到这一点。
我目前正在使用Scripting.FileSystemObject
来检查文件是否存在。
感谢。
答案 0 :(得分:11)
我使用的函数与前一个示例略有修改。该函数获取路径和文件名并返回“产品版本”
Function GetProductVersion (sFilePath, sProgram)
Dim FSO,objShell, objFolder, objFolderItem, i
Set FSO = CreateObject("Scripting.FileSystemObject")
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
我发现在XP和Vista中属性的位置偶尔会发生变化(不确定原因),所以我找到“产品版本”属性并在找到后退出循环。注释掉的行将显示所有属性和值(如果可用)
答案 1 :(得分:3)
您可以使用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版本而更改。您可以使用上面的代码来确定它们是什么。从我的测试来看,我相信它们如下:
您可能还会发现以下post有用。
答案 2 :(得分:0)
我认为你不能在vbScript中做到这一点。我可能错了。
这是作为执行所要求的PowerShell脚本的链接。
以下是我的windows dir的输出结果。
PS Scripts:\> ls c:\windows\*.exe | .\get-fileversion.ps1
ProductVersion FileVersion FileName
-------------- ----------- --------
2.7.3.0 2.7.3.0 C:\windows\agrsmdel.exe
答案 3 :(得分:0)
Wscript.Echo CreateObject("Scripting.FileSystemObject").GetFileVersion("C:\Windows\notepad.exe")
答案 4 :(得分: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