【问题标题】:How can I get the properties of an application file from the command line如何从命令行获取应用程序文件的属性
【发布时间】:2025-12-10 05:15:01
【问题描述】:

我正在尝试获取 .exe 文件的属性。

如果我使用wmic datafile where name="${localPath}" get Version, 我可以得到应用程序的版本。

但是,我想要属性中的product version 字段,并且无法直接从命令行获取它。 WMIC 能给我的只有:
AccessMask,存档,标题,压缩,CompressionMethod,CreationClassName,CreationDate,CSCreationClassName,CSName,描述,驱动器,EightDotThreeFileName,加密,EncryptionMethod,扩展名,文件名,文件大小,文件类型,FSCreationClassName,FSName,隐藏,InstallDate,InUseCount,LastAccessed,LastModified,制造商、名称、路径、可读、状态、系统、版本和可写。

有人知道怎么做吗?

【问题讨论】:

    标签: windows command-line wmic


    【解决方案1】:

    我结束了使用 powershell: powershell.exe Get-ItemProperty myApplication.exe -Name VersionInfo

    然后我能够解析节点中调用的输出。

    【讨论】:

      【解决方案2】:

      您可以在同一个批处理文件中获取带有 powershell 和 WMIC 的产品版本。

      只需将此代码复制并保存为 Get_Application_Version.bat

      @Echo Off
      Mode 80,10 & color 0A
      Title Version With PowerShell
      ::https://*.com/questions/49603697/windows-powershell-get-program-version-into-variable/49607234#49607234
      Set "AppFullPath=%Windir%\notepad.exe"
      For /F "Delims=" %%A In ('Powershell -C "(GI '%AppFullPath%').VersionInfo.ProductVersion"') Do Set "PV=%%A"
      Echo Version of this Application "%AppFullPath%"=%PV%
      Pause
      Title Get File Version of any Application using WMIC
      Rem WMIC
      Set "Version="
      Set "AppFullPath=%Windir%\notepad.exe"
      Call :Get_AppName "%AppFullPath%" AppName
      Call :Add_backSlash "%AppFullPath%"
      Call :GetVersion %Application% Version
      If defined Version (
          echo Vesrion of %AppName% ==^> %Version%
      )
      pause>nul & Exit
      ::*******************************************************************
      :Get_AppName <FullPath> <AppName>
      Rem %1 = FullPath
      Rem %2 = AppName
      for %%i in (%1) do set "%2=%%~nxi"
      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
      ::*******************************************************************
      :GetVersion <ApplicationPath> <Version>
      Rem The argument %~1 represent the full path of the application
      Rem without the double quotes
      Rem The argument %2 represent 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
      ::*******************************************************************
      

      【讨论】: