Windows:命令行读取可执行文件的版本信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25648155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:41:28  来源:igfitidea点击:

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

windowsbatch-filecommand-lineversionportable-executable

提问by neves

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

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

I see a lot of questions that show how to do it from different languages, and references to third party software to write it, but I can't find a simple shell command to do it. Additional points if I don't need to install anything.

我看到很多问题表明如何使用不同的语言进行操作,并参考了第三方软件来编写它,但我找不到一个简单的 shell 命令来执行此操作。如果我不需要安装任何东西,附加点。

It must be run as normal user. Not administrator.

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

回答by MC ND

wmic datafile where name="C:\Windows\System32\msiexec.exe" get Version /value 

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

你可以用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

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

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

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

编辑以适应评论,不需要管理员权限。在这种情况下,混合 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)

回答by lit

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.

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

(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:

如果您必须在 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"

回答by bviktor

set EXE='c:\firefox\firefox.exe'
powershell "(Get-Item -path %EXE%).VersionInfo.ProductVersion"

回答by MrCalvin

This will give you only the file version:

这只会给你文件版本:

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

Result:

结果:

Version
6.1.7601.18917

回答by npocmaka

and one way with makecab:

和一种方式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 :) ):

示例输出(它有一个字符串版本,这是 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

also you can take a look at tooltipinfo.bat

你也可以看看tooltipinfo.bat