我是批处理脚本方面的新手,正在寻找一种使用 cmd 上的命令提取 .exe 文件的“产品版本”字段的方法。
我确实设法使用此命令获取文件的常规“版本”字段:
wmic datafile where Name="C:\\Users\\MyProgram.exe" get Version
有没有办法修改这个命令行来提取“产品版本”字段?
任何其他根本没有 wmic 的解决方案也很好:) 谢谢。
您可以尝试使用此批处理文件轻松获取您的产品版本;在此示例中,我尝试使用 Google chrome :
@echo off
Title Get Product Version Using WMIC and Batch
Set FullPath=C:\Program Files\Google\Chrome\Application\chrome.exe
Call :Get_AppName "%FullPath%" AppName
Call :GetVersion "%FullPath%" Version
If defined Version (
echo "%FullPath%" ==^> v%Version%
echo "%AppName%" ==^> %Version%
)
Pause
EXIT /B
::----------------------------------------------------------------------------------------
:GetVersion <FullPathApp> <Version>
Rem The argument %~1 represent the full path of the application without the double quotes
Rem The argument %2 represent the variable to be set (in our case %2=Version)
Set "FullPathApp=%~1"
Set "FullPathApp=%FullPathApp:\=\\%"
FOR /F "tokens=2 delims==" %%I IN (
'wmic datafile where "name='%FullPathApp%'" get version /Value 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::----------------------------------------------------------------------------------------
:Get_AppName <FullPath> <AppName>
Rem %1 = FullPath
Rem %2 = AppName
for %%i in (%1) do set "%2=%%~nxi"
exit /b
::---------------------------------------------------------------------------------------
或者您可以尝试使用 Powershell 和批处理,灵感来自 在 PowerShell 中获取文件版本:
@echo off
Title Get Product Version using Powershell and Batch
Set FullPath=C:\Program Files\Google\Chrome\Application\chrome.exe
echo %FullPath%
Powershell -C "(Get-Item '%FullPath%').VersionInfo.FileVersion"
Pause
我假设它是“产品版本”字段,显示在您正在查找的 exe 文件的“属性”对话框的“详细信息”选项卡中。
这就是
C:\Windows\System32\notepad.exe
的样子:
基于对原始问题的 Qwertys 评论,您无法获得 ProductVersion ,只能获得 FileVersion (即“版本”)。
以notpad.exe为例
下面的示例展示了如何列出所有可以使用
wmic
, 查询的值
C:\Users\Operator>wmic datafile where name='c:\\windows\\system32\\notepad.exe' get /value
AccessMask=1179817
Archive=TRUE
Caption=c:\windows\system32\notepad.exe
Compressed=FALSE
CompressionMethod=
CreationClassName=CIM_LogicalFile
CreationDate=20221020203835.352020+000
CSCreationClassName=Win32_ComputerSystem
CSName=PCS-SES-A
Description=c:\windows\system32\notepad.exe
Drive=c:
EightDotThreeFileName=c:\windows\system32\notepad.exe
Encrypted=FALSE
EncryptionMethod=
Extension=exe
FileName=notepad
FileSize=201216
FileType=Application
FSCreationClassName=Win32_FileSystem
FSName=NTFS
Hidden=FALSE
InstallDate=20221020203835.352020+000
InUseCount=
LastAccessed=20231115103558.131866+000
LastModified=20221020203835.352020+000
Manufacturer=Microsoft Corporation
Name=c:\windows\system32\notepad.exe
Path=\windows\system32\
Readable=TRUE
Status=OK
System=FALSE
Version=10.0.19041.1865
Writeable=TRUE
可以使用 Powershell 检索“产品版本”字段,如下所示:
Powershell -C "(Get-Item 'c:\\windows\\system32\\notepad.exe').VersionInfo.ProductVersion"
如果您想使用批处理脚本,您将面临将值作为批处理脚本中的变量取回的挑战。看起来这个答案描述了如何做到这一点:如何将变量从 PowerShell 传递到批处理变量