Rob van der Woude's Scripting Pages

Batch How To ...

Read File and Product Versions

Every current Windows version features the FILEVER command to read files' file and product version:

Prints file version information.

filever [/S] [/V] [/E] [/X] [/B] [/A] [/D] [[drive:][path][filename]]

/S	Displays files in specified directory and all subdirectories.
/V	List verbose version information if available.
/E	List executables only.
/X	Displays short names generated for non-8dot3 file names.
/B	Uses bare format (no dir listing).
/A	Don't display file attributes.
/D	Don't display file date and time.

Its /V switch reveals lots of file details:

D:\>filever /v %windir%\system32\cacls.exe
--a-- W32i   APP ENU  6.1.7600.16385 shp     25,600 07-14-2009 cacls.exe
        Language        0x0409 (English (United States))
        CharSet         0x04b0 Unicode
        OleSelfRegister Disabled
        CompanyName     Microsoft Corporation
        FileDescription Control ACLs Program
        InternalName    cacls
        OriginalFilenam CACLS.EXE
        ProductName     Microsoft™ Windows™ Operating System
        ProductVersion  6.1.7600.16385
        FileVersion     6.1.7600.16385 (win7_rtm.090713-1255)
        LegalCopyright  © Microsoft Corporation. All rights reserved.

        Signature:      feef04bd
        Struc Ver:      00010000
        FileVer:        00060001:1db04001 (6.1:7600.16385)
        ProdVer:        00060001:1db04001 (6.1:7600.16385)
        FlagMask:       0000003f
        Flags:          00000000
        OS:             00040004 NT Win32
        FileType:       00000001 App
        SubType:        00000000
        FileDate:       00000000:00000000


To extract the product version we can use a FOR /F loop:

FOR /F "tokens=2" %%A IN ('FILEVER /V %windir%\System32\cacls.exe ^" FIND "ProductVersion"') DO SET ProductVersion=%%A

Note, however, that the term ProductVersion is language dependent, i.e. you have to adapt FIND's search string to the OS language.

If you cannot be sure about the OS language, your safest bet is to use the file version:

FOR /F "tokens=5" %%A IN ('FILEVER %windir%\system32\cacls.exe') DO SET FileVersion=%%A

Note that we did not use the /V switch, nor did we need FIND.

To compare two versions, we cannot just use IF version1 GTR version2 because that would only work on integers; if the versions compared contain dots, the IF statement performs a string comparison, making "9.10" greater than "10.9".
We have to compare the versions one digit at a time, until a difference is found.

I wrote CompareVersions.bat to perform a proper comparison, digit by digit.
It required quite a lot of code to accomplish this, mainly because leading zeroes may make an IF version1_digit_n GTR version2_digit_n test err or even crash.


page last uploaded: 2017-02-28, 23:45