【问题标题】:Windows: Command line to read version info of an executable file?Windows:命令行读取可执行文件的版本信息?
【发布时间】:2014-09-03 15:24:24
【问题描述】:

Windows 是否有一个可执行文件可以在命令 shell 中运行,它返回可执行文件 (.exe) 文件的版本号?

我看到很多问题显示如何从不同的语言中做到这一点,并参考了第三方软件来编写它,但我找不到一个简单的 shell 命令来做到这一点。如果我不需要安装任何东西,请加分。

它必须以普通用户身份运行。不是管理员。

【问题讨论】:

标签: windows batch-file command-line version portable-executable


【解决方案1】:
wmic datafile where name="C:\\Windows\\System32\\msiexec.exe" get Version /value 

您可以使用wmic 来执行此操作。你可以把它包装成一个批处理文件

@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

将其保存为(示例)getVersion.cmd 并调用为getVersion.cmd "c:\windows\system32\msiexec.exe"

编辑以适应 cmets 并且不需要管理员权限。在这种情况下,使用混合 cmd/javascript 文件来查询 wmi。一样的用法

@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)

【讨论】:

  • 很好,但不幸的是,您必须在管理员组中才能运行它:-( 我需要以我的持续集成用户身份运行它。
  • 之后你如何使用%vers%?当我尝试echo %vers% 时,命令提示符会通知我ECHO is off.
  • @AlainD,setlocal / endlocal 防止环境中的更改传播到批处理文件之外。如果您需要 %vers% 值,请将结尾的 endlocal 行更改为 enlocal & set "vers=%vers%"
  • @mc-nd 小错字:enDlocal & set "vers=%vers%
  • @AndrewDennison,你是对的,谢谢。不幸的是,我不允许编辑评论。
【解决方案2】:

如果您愿意并且能够使用 PowerShell,以下代码将起作用。如果您使用的是受支持的 Windows 系统,则可以使用 PowerShell。

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

如果你必须在 cmd.exe shell 中运行它,你可以使用:

powershell -NoLogo -NoProfile -Command ^
    "(Get-Item -Path 'C:\Program Files (x86)\Java\jre1.8.0_201\bin\java.exe').VersionInfo |" ^
        "Format-List -Force"

【讨论】:

  • 这是最简单最清晰的答案,同时直接适用于PS,没有任何其他依赖。
【解决方案3】:
set EXE='c:\firefox\firefox.exe'
powershell "(Get-Item -path %EXE%).VersionInfo.ProductVersion"

【讨论】:

  • 虽然这个纯代码的答案可能会回答这个问题,但请添加解释为什么这样做。这将帮助未来的用户根据他们的情况评估答案。
  • "添加解释为什么这样做" 因为它是为了这样做而设计的?这些模板问题毫无意义。
  • 否,因为您已设置当前活动的 exe 文件 powershell 将使用第一行解释,然后您在当前活动的 exe 上使用了 powershell 命令。
  • 什么?我在几个 Jenkins 工作中积极使用这个 sn-p,但请告诉我它是如何不工作的,或者其他什么......
【解决方案4】:

这只会给你文件版本:

wmic datafile where name='c:\\windows\\system32\\notepad.exe' get version

结果:

Version
6.1.7601.18917

【讨论】:

  • 您好,仅代码的答案往往会被标记为审核。你能添加一行解释这是什么吗?
  • 这个答案应该是最重要的。与当前接受的答案不同,简明扼要,更不用说工作了!
【解决方案5】:

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*"

示例输出(它有一个字符串版本,是 wmic 方法的一个小补充 :)):

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

你也可以看看tooltipinfo.bat

【讨论】:

    【解决方案6】:

    filever c:\windows\system32\notepad.exe (filever 预装在每个 Windows 操作系统上)。

    【讨论】:

    • 我输入了where filever,但什么也没找到。
    • 显然,filever 在 Windows 10 上不再可用。虽然 OP 没有指定操作系统版本,但我在此处添加此评论以供以后的读者阅读。
    【解决方案7】:

    filever.exe 位于 Windows 2003 支持工具的 SUPPORT.CAB 中,也可能位于其他位置。

    从 CMD 脚本中使用 VBScript 和 Scripting.FileSystemObject 的方法

    @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%
    

    powershell 方法的变体,如果您从 CMD 脚本调用。 使用 FileVersionRaw 代替 FileVersion,因为 FileVersion 可以有额外的文本修饰,但必须 ToString() 才能获得预期的格式。

    @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%
    

    来自 CMD 的 Powershell 方法来比较版本,因为这可能是首先询问的原因。 必须使用 %ErrorLevel%==x 因为 ErrorLevel==x 实际上大于或等于。

    @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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      • 2015-04-24
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多