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

Windows:读取可执行文件版本信息的命令行?,windows,batch-file,command-line,version,portable-executable,Windows,Batch File,Command Line,Version,Portable Executable,Windows是否有可以在返回可执行(.exe)文件版本号的命令shell中运行的可执行文件 我看到了很多问题,这些问题说明了如何从不同的语言来实现它,并引用了第三方软件来编写它,但我找不到一个简单的shell命令来实现它。如果我不需要安装任何东西,请提供附加点 它必须作为普通用户运行。不是管理员 wmic datafile where name="C:\\Windows\\System32\\msiexec.exe" get Version /value 您可以使用wmic来执行此操作。您

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

我看到了很多问题,这些问题说明了如何从不同的语言来实现它,并引用了第三方软件来编写它,但我找不到一个简单的shell命令来实现它。如果我不需要安装任何东西,请提供附加点

它必须作为普通用户运行。不是管理员

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”

已编辑以适应评论,不需要管理员权限。在本例中,使用混合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)

和带有
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方法的一个小的补充:):


此外,如果您愿意并且能够使用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"

这将只提供文件版本:

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

Version
6.1.7601.18917

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

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%
如果从CMD脚本调用,则为powershell方法的变体。 使用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

可能与Great重复,但很遗憾,您必须在管理员组中才能运行它:-(我需要以我的持续集成用户身份运行它。以后如何使用%vers%?当我尝试
echo%vers%
时,命令提示符通知我
echo已关闭。
@AlainD,
setlocal/endlocal
防止环境中的更改传播到批处理文件之外。如果需要
%vers%
值,请更改结束
endlocal
行至
enlocal并设置“vers=%vers%”
@mc nd小字体:endlocal&set"vers=%vers%@AndrewDennison,你说得对,谢谢。不幸的是,我不允许编辑评论。嗨,只有代码的答案往往会被标记为适度。你能添加一行解释这有什么用吗?这个答案应该放在最上面。简短、简明扼要、切中要害,更不用说工作了,这与当前接受的答案不同!而这个代码-“只有答案”可以回答该问题,请添加解释说明其原因。这将有助于未来用户根据自己的情况评估答案。“添加解释说明其原因”因为它是设计用来这样做的?这些模板问题是毫无意义的。不,因为您已经设置了当前活动的exe文件,powershell将用第一行解释,然后您在当前活动的exe上使用了powershell命令。什么?我在几个Jenkins作业中积极使用此代码段,但请告诉我它是如何工作的,或者什么tever…这是最简单和最清楚的答案,但直接适用于PS,没有任何其他依赖项。我键入了
where filever
,但什么也找不到。显然,filever在Windows 10上不再可用。虽然OP没有指定操作系统版本,但我在此处添加此注释,以供将来的读者使用。
@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%
@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