How to retrieve file informations ?

Windows specific forum
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

How to retrieve file informations ?

Post by Flype »

When doing 'right click on a file', then 'properties', then click on page 'version' we have some informations on a file (version,company,etc.) how to retrieve those informations ?

informations i need :

Code: Select all

Enumeration 
  #SHOP_PRINTERNAME
  #SHOP_VOLUMEGUID
  #SHOP_FILEPATH
EndEnumeration

Import "shell32.lib"
  SHObjectProperties(hWnd.l, type.l, object.p-unicode, page.p-unicode)
EndImport

fileName.s = Space(#MAX_PATH)
ExpandEnvironmentStrings_("%windir%\notepad.exe", @fileName, #MAX_PATH)
SHObjectProperties(0, #SHOP_FILEPATH, fileName, "version")

Delay(2000)
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Code: Select all

;/ Author : Sverson 
;/ Get exe/dll file information [gfvi] 

; FieldName = #True : Add Field Name / #False : Without Field Name

; Wich = 
; #GFVI_FileVersion / #GFVI_FileDescription / #GFVI_LegalCopyright / #GFVI_InternalName / #GFVI_OriginalFilename 
; #GFVI_ProductName / #GFVI_ProductVersion / #GFVI_CompanyName / #GFVI_LegalTrademarks / #GFVI_SpecialBuild
; #GFVI_PrivateBuild / #GFVI_Comments / #GFVI_Language
; Or #GFVI_All if you want to retrieve all these fields


Enumeration 
  #GFVI_FileVersion      = $0001 
  #GFVI_FileDescription  = $0002 
  #GFVI_LegalCopyright   = $0004 
  #GFVI_InternalName     = $0008 
  #GFVI_OriginalFilename = $0010 
  #GFVI_ProductName      = $0020 
  #GFVI_ProductVersion   = $0040 
  #GFVI_CompanyName      = $0080 
  #GFVI_LegalTrademarks  = $0100 
  #GFVI_SpecialBuild     = $0200 
  #GFVI_PrivateBuild     = $0400 
  #GFVI_Comments         = $0800 
  #GFVI_Language         = $1000 
  #GFVI_All              = $1FFF 
EndEnumeration 

Procedure.s GetElementNameInternal(elementKey.l)
  If     elementKey = #GFVI_FileVersion      : ProcedureReturn "FileVersion" 
  ElseIf elementKey = #GFVI_FileDescription  : ProcedureReturn "FileDescription" 
  ElseIf elementKey = #GFVI_LegalCopyright   : ProcedureReturn "LegalCopyright" 
  ElseIf elementKey = #GFVI_InternalName     : ProcedureReturn "InternalName" 
  ElseIf elementKey = #GFVI_OriginalFilename : ProcedureReturn "OriginalFilename" 
  ElseIf elementKey = #GFVI_ProductName      : ProcedureReturn "ProductName" 
  ElseIf elementKey = #GFVI_ProductVersion   : ProcedureReturn "ProductVersion" 
  ElseIf elementKey = #GFVI_CompanyName      : ProcedureReturn "CompanyName" 
  ElseIf elementKey = #GFVI_LegalTrademarks  : ProcedureReturn "LegalTrademarks" 
  ElseIf elementKey = #GFVI_SpecialBuild     : ProcedureReturn "SpecialBuild" 
  ElseIf elementKey = #GFVI_PrivateBuild     : ProcedureReturn "PrivateBuild" 
  ElseIf elementKey = #GFVI_Comments         : ProcedureReturn "Comments" 
  ElseIf elementKey = #GFVI_Language         : ProcedureReturn "Language" 
  EndIf 
EndProcedure 

ProcedureDLL.s GetFileVersion(FileName.s,Which,FieldName)
  Protected lpdwHandle.l, dwLen.w, lpData.l, lplpBuffer.l, puLen.l, *pBlock, lpSubBlock$ 
  Protected nsize.w, szLang$, bBit.b, lekFlag.l, sElement$, sGFVI$ 
  
  lplpBuffer = 0 : puLen = 0 : sGFVI$ = "" : nsize = 128 : szLang$ = Space(nsize) 
  
  If FileSize(FileName.s)>0 
    If OpenLibrary(1,"Version.dll") 
      dwLen = CallFunction(1,"GetFileVersionInfoSizeA",FileName.s,@lpdwHandle) 
      If dwLen>0 
        *pBlock=AllocateMemory(dwLen) 
        If *pBlock>0 
          Result = CallFunction(1,"GetFileVersionInfoA",FileName.s,0,dwLen,*pBlock) 
          If Result 
            lpSubBlock$ = "\\VarFileInfo\\Translation" 
            Result      = CallFunction(1,"VerQueryValueA",*pBlock,lpSubBlock$,@lplpBuffer,@puLen) 
            If Result 
              CPLI$  = RSet(Hex(PeekW(lplpBuffer)),4,"0")+RSet(Hex(PeekW(lplpBuffer+2)),4,"0") 
              Result = CallFunction(1,"VerLanguageNameA",PeekW(lplpBuffer),@szLang$,nsize) 
            EndIf 
            lekFlag = 1 
            For bBit = 1 To 12 
              If lekFlag & Which 
                sElement$   = GetElementNameInternal(lekFlag) 
                lpSubBlock$ = "\\StringFileInfo\\"+CPLI$+"\\"+sElement$ 
                Result      = CallFunction(1,"VerQueryValueA",*pBlock,lpSubBlock$,@lplpBuffer,@puLen) 
                If Result 
                  If sGFVI$<>"" : sGFVI$+Chr(10) : EndIf 
                  If FieldName 
                    sGFVI$=sGFVI$+sElement$+":"+Chr(9)+PeekS(lplpBuffer) 
                  Else 
                    sGFVI$=sGFVI$+PeekS(lplpBuffer) 
                    
                  EndIf 
                  
                EndIf 
              EndIf 
              lekFlag << 1 
            Next 
            If lekFlag & Which 
              If sGFVI$<>"" : sGFVI$+Chr(10) : EndIf 
              If FieldName 
                sElement$ = GetElementNameInternal(lekFlag) 
                sGFVI$    = sGFVI$+sElement$+":"+Chr(9)+szLang$ 
              Else 
                sGFVI$    = sGFVI$+szLang$ 
              EndIf 
            EndIf 
          EndIf 
          FreeMemory(*pBlock) 
        EndIf 
      EndIf 
      CloseLibrary(1) 
    EndIf 
  EndIf 
  ProcedureReturn sGFVI$ 
EndProcedure 


;/ Test
; File.s  = "c:\windows\regedit.exe"
; MessageRequester(GetFilePart(File)+" (with FieldName)",GetFileVersion(File,#GFVI_CompanyName,#True))
; MessageRequester(GetFilePart(File)+" (without FieldName)",GetFileVersion(File,#GFVI_CompanyName,#False))
; MessageRequester(GetFilePart(File)+" (without FieldName)",GetFileVersion(File,#GFVI_FileVersion | #GFVI_FileDescription,#False))
; MessageRequester(GetFilePart(File)+" (with FieldName)",GetFileVersion(File,#GFVI_All,#True))
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

Thank you Droopy for the very fast answer :wink:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Thanks to Sverson too :wink:
Post Reply