如何从Windows Vista上的批处理文件中检索文件的版本?

问题描述 投票:13回答:7

二进制文件中嵌入了一个版本 - 易于在Windows资源管理器中显示。

如何从批处理文件中检索该文件版本?

windows-vista batch-file version
7个回答
23
投票

以及没有外部工具的三种方式

1.WMIC

WMIC DATAFILE WHERE name="C:\\install.exe" get Version /format:Textvaluelist

注意文件名的双斜杠。

随时可用的脚本:

@echo off
:wmicVersion pathToBinary [variableToSaveTo]
setlocal
set "item=%~1"
set "item=%item:\=\\%"


for /f "usebackq delims=" %%a in (`"WMIC DATAFILE WHERE name='%item%' get Version /format:Textvaluelist"`) do (
    for /f "delims=" %%# in ("%%a") do set "%%#"
)

if "%~2" neq "" (
    endlocal & (
        echo %version%
        set %~2=%version%
    )
) else (
    echo %version%
)

2.MAKECAB,因为WMIC没有安装在Windows的家庭版本上,这是一种可以在每台Windows机器上运行的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

3使用shell.application和hybrid batch \ jscript.Here的tooptipInfo.bat

@if (@X)==(@Y) @end /* JScript comment
    @echo off

    rem :: the first argument is the script name as it will be used for proper help message
    cscript //E:JScript //nologo "%~f0" %*

    exit /b %errorlevel%

@if (@X)==(@Y) @end JScript comment */

////// 
FSOObj = new ActiveXObject("Scripting.FileSystemObject");
var ARGS = WScript.Arguments;
if (ARGS.Length < 1 ) {
 WScript.Echo("No file passed");
 WScript.Quit(1);
}
var filename=ARGS.Item(0);
var objShell=new ActiveXObject("Shell.Application");
/////


//fso
ExistsItem = function (path) {
    return FSOObj.FolderExists(path)||FSOObj.FileExists(path);
}

getFullPath = function (path) {
    return FSOObj.GetAbsolutePathName(path);
}
//

//paths
getParent = function(path){
    var splitted=path.split("\\");
    var result="";
    for (var s=0;s<splitted.length-1;s++){
        if (s==0) {
            result=splitted[s];
        } else {
            result=result+"\\"+splitted[s];
        }
    }
    return result;
}


getName = function(path){
    var splitted=path.split("\\");
    return splitted[splitted.length-1];
}
//

function main(){
    if (!ExistsItem(filename)) {
        WScript.Echo(filename + " does not exist");
        WScript.Quit(2);
    }
    var fullFilename=getFullPath(filename);
    var namespace=getParent(fullFilename);
    var name=getName(fullFilename);
    var objFolder=objShell.NameSpace(namespace);
    var objItem=objFolder.ParseName(name);
    //https://msdn.microsoft.com/en-us/library/windows/desktop/bb787870(v=vs.85).aspx
    WScript.Echo(fullFilename + " : ");
    WScript.Echo(objFolder.GetDetailsOf(objItem,-1));

}

main();

用于对cmd.exe:

C:\Windows\System32\cmd.exe :
File description: Windows Command Processor
Company: Microsoft Corporation
File version: 6.3.9600.16384
Date created: ?22-?Aug-?13 ??13:03
Size: 347 KB

6
投票

查看sigcheck.exeSysinternals Suite。这是一个命令行实用程序,可显示文件版本号,时间戳信息和数字签名详细信息。


4
投票

PowerShell可以在没有外部工具的情况下简单地读取文件版本,如下所示:

(Get-Item myFile.exe).VersionInfo.FileVersion

如果要从批处理文件中读取它,请使用:

powershell -NoLogo -NoProfile -Command (Get-Item myFile.exe).VersionInfo.FileVersion

并将文件版本保存到变量使用:

FOR /F "USEBACKQ" %%F IN (`powershell -NoLogo -NoProfile -Command ^(Get-Item "myFile.exe"^).VersionInfo.FileVersion`) DO (SET fileVersion=%%F)
echo File version: %fileVersion%

我在这里采取了相对路径,你也可以绝对定义它,如:qazxsw poi

如果省略C:\myFile.dll,则会显示有关该文件的更多信息


3
投票

这是使用FileVersionInfo的.FileVersion


3
投票

我认为solution in PowerShell是你需要的。它可以同时获取多个项目的文件版本,并找到大小或版本号不同的文件(例如EXE,DLL)。


2
投票

这是我尝试使用filever获取Skype目录中所有WMIC*.exe的文件版本作为示例:

*.dll

0
投票

我从@echo off Mode 75,3 & color 9E Title Get File Version of any Program from file list using WMIC by Hackoo Set "RootFolder=%ProgramFiles%\Skype" @for %%a in (%RootFolder%) do set "FolderName=%%~na" Set "File_Version_List=%~dp0%FolderName%_File_Version_List.txt" Set "ErrorFile=%~dp0%FolderName%_Error.txt Set Extensions="*.exe" "*.dll" If exist "%ErrorFile%" Del "%ErrorFile%" If exist "%File_Version_List%" Del "%File_Version_List%" echo( echo Please wait a while ... Process to get file version ... set "BuildLineWith=call :BuildLine " setlocal enabledelayedexpansion CD /D "%RootFolder%" @for /f "delims=" %%F in ('Dir /b /s %Extensions%') do ( set "Version=" Call :Get_AppName "%%F" AppName Call :Add_backSlash "%%F" Call :GetVersion !Application! Version Call :Remove_backSlash !Application! If defined Version ( ( echo !Application! echo !AppName! ==^> !Version! %BuildLineWith%* )>> "%File_Version_List%" ) else ( ( echo Version is not defined in !Application! %BuildLineWith%# )>> "%ErrorFile%" ) ) If Exist "%ErrorFile%" Start "" "%ErrorFile%" If Exist "%File_Version_List%" Start "" /MAX "%File_Version_List%" & Exit ::******************************************************************* :GetVersion <ApplicationPath> <Version> Rem The argument %~1 represente the full path of the application Rem without the double quotes Rem The argument %2 represente the variable to be set (in our case %2=Version) FOR /F "tokens=2 delims==" %%I IN ( 'wmic datafile where "name='%~1'" get version /format:Textvaluelist 2^>^nul' ) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A" Exit /b ::******************************************************************* :Add_backSlash <String> Rem Subroutine to replace the simple "\" by a double "\\" into a String Set "Application=%1" Set "String=\" Set "NewString=\\" Call Set "Application=%%Application:%String%=%NewString%%%" Exit /b ::******************************************************************* :Remove_backSlash <String> Rem Subroutine to replace the double "\\" by a simple "\" into a String Set "Application=%1" Set "String=\\" Set "NewString=\" Call Set "Application=%%Application:%String%=%NewString%%%" Exit /b ::******************************************************************* :Get_AppName <FullPath> <AppName> Rem %1 = FullPath Rem %2 = AppName for %%i in (%1) do set "%2=%%~nxi" exit /b ::******************************************************************* :BuildLine set "LineChar=%1" if "%LineChar%"=="" set "LineChar=_" for /f "tokens=2 skip=4" %%A in ('mode con: /status') do set "WindowColumns=%%A" & goto :GotColumnCount :GotColumnCount set "CharLine=" setlocal EnableDelayedExpansion for /L %%A in (1,1,%WindowColumns%) do set "CharLine=!CharLine!!LineChar:~0,1!" setlocal DisableDelayedExpansion endlocal echo %CharLine% goto :eof ::******************************************************************* 找到了这段代码:

Rob Vanderwoude's site

看起来很有趣,因为它使用来自SysInternals的STRINGS。

© www.soinside.com 2019 - 2024. All rights reserved.