9
votes

Comment récupérer "la version du produit" d'un fichier dans VBScript

J'ai un VBScript qui vérifie l'existence d'un fichier dans un répertoire sur une machine distante. Je cherche à extraire la version " version du produit " pour ledit fichier ( pas " version de fichier "), mais je ne peux pas sembler comprendre sur la façon de faire cela dans VBScript.

J'utilise actuellement scripting.filesystemObject pour vérifier l'existence du fichier.

Merci.


1 commentaires

5 Réponses :


-1
votes

Je ne pense pas que vous puissiez le faire dans VBScript. Je pourrais me tromper sur cela.

Voici comme un lien vers un script PowerShell qui fait ce que vous demandez.

Texte de la liaison

Voici ce que la sortie de mon Windows Dir ressemble. xxx


1 commentaires

Au lieu de ce script mystérieux, vous ne pouvez qu'utiliser: (get-item "c: \ chemin \ file.ext"). VersionInfo



3
votes

Vous pouvez utiliser le shell.namespace a > Pour obtenir le Propriétés étendues sur un fichier, dont l'une est la version du produit. Le getDétails de fonction doit fonctionner. Vous pouvez tester avec le code suivant pour avoir une idée:

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
  • Fenêtre XP - 39 LI>
  • Windows Vista - 252 Li>
  • Windows 7 - 268 LI>
  • Windows 2008 R2 SP1 - 271 LI>
  • Windows 2012 R2 - 285 LI> ul>

    Vous pouvez également trouver ce qui suit post utile. p> p>


1 commentaires

La première ligne de votre code "Dim Fillattributes (300)" n'a rien ajouté dans votre script. Vous devez remplacer "Fillattributes (i)" avec "folder.getDétails de (dossier.items, i)"



11
votes

J'utilise une fonction légèrement modifiée de l'exemple précédent. La fonction prend le chemin et le nom du fichier et renvoie la "Version du produit"

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


2 commentaires

Vous n'avez pas créé l'objet de la FSO pour l'utiliser dans votre fonction que vous devez ajouter "SET FSO = CreateObject (" scripting.filesystemObject ")" après la ligne 2 de votre fonction


Pourquoi 300? Serait-il préférable d'utiliser (2 ^ 15) - 1, qui est la valeur entière maximale?



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

2 commentaires

L'OP a dit Version du produit , pas version de fichier . Ce n'est pas la même chose.


Ce n'est pas techniquement correct mais il sert à mes besoins et est beaucoup plus concis que toutes les autres réponses.



0
votes
' 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

0 commentaires