Windows: Command line to read version info of an executable file?

前端 未结 7 628
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 02:23

Does Windows have an executable that I can run in the command shell which returns the version number of an executable (.exe) file?

I see a lot of questions that sho

相关标签:
7条回答
  • 2020-12-14 03:04

    and one way with makecab:

    ; @echo off
    ;;goto :end_help
    ;;setlocal DsiableDelayedExpansion
    ;;;
    ;;;
    ;;; fileinf /l list of full file paths separated with ;
    ;;; fileinf /f text file with a list of files to be processed ( one on each line )
    ;;; fileinf /? prints the help
    ;;;
    ;;:end_help
    
    ; REM Creating a Newline variable (the two blank lines are required!)
    ; set NLM=^
    
    
    ; set NL=^^^%NLM%%NLM%^%NLM%%NLM%
    ; if "%~1" equ "/?" type "%~f0" | find ";;;" | find /v "find" && exit /b 0
    ; if "%~2" equ "" type "%~f0" | find ";;;" | find /v "find" && exit /b 0
    ; setlocal enableDelayedExpansion
    ; if "%~1" equ "/l" (
    ;  set "_files=%~2"
    ;  echo !_files:;=%NL%!>"%TEMP%\file.paths"
    ;  set _process_file="%TEMP%\file.paths"
    ;  goto :get_info
    ; )
    
    ; if "%~1" equ "/f" if exist "%~2" (
    ;  set _process_file="%~2"
    ;  goto :get_info
    ; )
    
    ; echo incorect parameters & exit /b 1
    ; :get_info
    ; set "file_info="
    
    ; makecab /d InfFileName=%TEMP%\file.inf /d "DiskDirectory1=%TEMP%" /f "%~f0"  /f %_process_file% /v0>nul
    
    ; for /f "usebackq skip=4 delims=" %%f in ("%TEMP%\file.inf") do (
    ;  set "file_info=%%f"
    ;  echo !file_info:,=%nl%!
    ; )
    
    ; endlocal
    ;endlocal
    ; del /q /f %TEMP%\file.inf 2>nul
    ; del /q /f %TEMP%\file.path 2>nul
    ; exit /b 0
    
    .set DoNotCopyFiles=on
    .set DestinationDir=;
    .set RptFileName=nul
    .set InfFooter=;
    .set InfHeader=;
    .Set ChecksumWidth=8
    .Set InfDiskLineFormat=;
    .Set Cabinet=off
    .Set Compress=off
    .Set GenerateInf=ON
    .Set InfDiskHeader=;
    .Set InfFileHeader=;
    .set InfCabinetHeader=;
    .Set InfFileLineFormat=",file:*file*,date:*date*,size:*size*,csum:*csum*,time:*time*,vern:*ver*,vers:*vers*,lang:*lang*"
    

    example output (it has a string version which is a small addition to wmic method :) ):

    c:> fileinfo.bat /l C:\install.exe
        file:install.exe
        date:11/07/07
        size:562688
        csum:380ef239
        time:07:03:18a
        vern:9.0.21022.8
        vers:9.0.21022.8 built by: RTM
        lang:1033
    

    also you can take a look at tooltipinfo.bat

    0 讨论(0)
  • 2020-12-14 03:05
    set EXE='c:\firefox\firefox.exe'
    powershell "(Get-Item -path %EXE%).VersionInfo.ProductVersion"
    
    0 讨论(0)
  • 2020-12-14 03:15

    filever.exe is in SUPPORT.CAB from the Windows 2003 Support tools, and maybe other places.

    A method using VBScript and Scripting.FileSystemObject from a CMD script

    @Echo off
    Set Version=
    Echo WScript.Echo "Set Version=" ^& CreateObject("Scripting.FileSystemObject").GetFileVersion("%SystemRoot%\notepad.exe") >%temp%\~FileVer.vbs
    Cscript.exe //nologo %temp%\~FileVer.vbs>%temp%\~FileVer.cmd
    Call %temp%\~FileVer.cmd
    Echo Version=%Version%
    

    A variant of the powershell method, if you are calling from a CMD script. Using FileVersionRaw instead of FileVersion, because FileVersion can have extra text decoration, but have to ToString() to get the expected format.

    @Echo off
    Set Version=
    Powershell -c "'Set Version=' + (Get-Command '%SystemRoot%\Notepad.exe').FileVersionInfo.FileVersionRaw.ToString()">%temp%\~FileVer.cmd
    Call %temp%\~FileVer.cmd
    Echo Version=%Version%
    

    A Powershell from CMD method to compare versions, as that could be the reason for asking in the first place. Have to use %ErrorLevel%==x because ErrorLevel==x is actually greater or equal.

    @echo off
    Powershell -c "exit 10 + ((Get-Command '%SystemRoot%\Notepad.exe').FileVersionInfo.FileVersionRaw).CompareTo([System.Version] '10.0.19041.1')"
    If %ErrorLevel%==9  Echo File needs updating
    If %ErrorLevel%==10 Echo File is expected version
    If %ErrorLevel%==11 Echo File is newer than expected
    
    0 讨论(0)
  • 2020-12-14 03:16
    wmic datafile where name="C:\\Windows\\System32\\msiexec.exe" get Version /value 
    

    You can use wmic to do it. And you can wrap it into a batch file

    @echo off
        setlocal enableextensions
    
        set "file=%~1"
        if not defined file goto :eof
        if not exist "%file%" goto :eof
    
        set "vers="
        FOR /F "tokens=2 delims==" %%a in ('
            wmic datafile where name^="%file:\=\\%" get Version /value 
        ') do set "vers=%%a"
    
        echo(%file% = %vers% 
    
        endlocal
    

    Save it as (example) getVersion.cmd and call as getVersion.cmd "c:\windows\system32\msiexec.exe"

    edited to adapt to comments and not require administrator rights. In this case, an hybrid cmd/javascript file is used to query wmi. Same usage

    @if (@this==@isBatch) @then
    @echo off
        setlocal enableextensions
    
        set "file=%~f1"
        if not exist "%file%" goto :eof
    
        cscript //nologo //e:jscript "%~f0" /file:"%file%"
    
        endlocal
    
        exit /b
    @end
        var file = WScript.Arguments.Named.Item('file').replace(/\\/g,'\\\\');
        var wmi = GetObject('winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2')
        var files = new Enumerator(wmi.ExecQuery('Select Version from CIM_datafile where name=\''+file+'\'')) 
    
        while (!files.atEnd()){
            WScript.StdOut.WriteLine(files.item().Version);
            files.moveNext();
        };
        WScript.Quit(0)
    
    0 讨论(0)
  • 2020-12-14 03:17

    filever c:\windows\system32\notepad.exe (the filever is preinstalled on every Windows OS).

    0 讨论(0)
  • 2020-12-14 03:21

    If you are willing and able to use PowerShell, the following code will work. If you are on a supported Windows system, PowerShell will be available.

    (Get-Item -Path 'C:\Program Files\Java\jdk1.8.0_144\bin\java.exe').VersionInfo |
        Format-List -Force
    

    If you must run it in a cmd.exe shell, you could use:

    powershell -NoLogo -NoProfile -Command ^
        "(Get-Item -Path 'C:\Program Files (x86)\Java\jre1.8.0_201\bin\java.exe').VersionInfo |" ^
            "Format-List -Force"
    
    0 讨论(0)
提交回复
热议问题