views:

1042

answers:

4

Hi,

Last time I asked a similar question but that was about svn related versioning info. Now I am wondering how to query windows "File version" attribute about eg. a dll. I payed attention to wmi and win32file modules as well without success. Thanks U in advance. D.

+2  A: 

You can use the filever.exe tool to do that. Here'a thread that explains how to do it from Python. And here's the KB article with details about the tool.

Franci Penov
+1  A: 

I found this solution at "timgolden" site. Works fine.

from win32api import GetFileVersionInfo, LOWORD, HIWORD

def get_version_number (filename):
  info = GetFileVersionInfo (filename, "\\")
  ms = info['FileVersionMS']
  ls = info['FileVersionLS']
  return HIWORD (ms), LOWORD (ms), HIWORD (ls), LOWORD (ls)

if __name__ == '__main__':
  import os
  filename = os.environ["COMSPEC"]
  print ".".join ([str (i) for i in get_version_number (filename)])
+2  A: 

Better to add a try/except in case the file has no version number attribute.

filever.py


from win32api import GetFileVersionInfo, LOWORD, HIWORD

def get_version_number (filename):
    try:
     info = GetFileVersionInfo (filename, "\\")
     ms = info['FileVersionMS']
     ls = info['FileVersionLS']
     return HIWORD (ms), LOWORD (ms), HIWORD (ls), LOWORD (ls)
    except:
     return 0,0,0,0

if __name__ == '__main__':
  import os
  filename = os.environ["COMSPEC"]
  print ".".join ([str (i) for i in get_version_number (filename)])

yourscript.py:


import os,filever

myPath="C:\\path\\to\\check"

for root, dirs, files in os.walk(myPath):
    for file in files:
     file = file.lower() # Convert .EXE to .exe so next line works
     if (file.count('.exe') or file.count('.dll')): # Check only exe or dll files
      fullPathToFile=os.path.join(root,file)
      major,minor,subminor,revision=filever.get_version_number(fullPathToFile)
      print "Filename: %s \t Version: %s.%s.%s.%s" % (file,major,minor,subminor,revision)

Cheers!

Jamie
A: 

You can use the pyWin32 module from http://sourceforge.net/projects/pywin32/:

from win32com.client import Dispatch

ver_parser = Dispatch('Scripting.FileSystemObject')
info = ver_parser.GetFileVersion(path)

if info == 'No Version Information Available':
    info = None
the_void