How to get File version in variable using .cmd

谁说我不能喝 提交于 2019-12-08 03:09:45

问题


I used wmic to get the version of a EXE stored in a variable. It works fine when I execute it from CMD (that I ran as Admin) but when executed either in a .cmd or .bat files (even when ran as admin) the variable is not populated.

This works directly in CMD:

set "npver="&for /F "tokens=2delims==" %a in ('wmic datafile where "Name='c:\\Windows\\System32\\notepad.exe'" get version /format:list') do set "npver=%a"

When I created the .cmd file, I doubled the % signs.


回答1:


You forgot to echo the variable with the command echo %npver%

With Command line :

set "npver="&for /F "tokens=2delims==" %a in ('wmic datafile where "Name='c:\\Windows\\System32\\notepad.exe'" get version /format:list') do set "npver=%a"& echo %npver%

With a batch file, you should escape the sign percent % by adding another one %% and your code should works now with a batch file like this :

Batch File :

@echo off
set "npver="&for /F "tokens=2delims==" %%a in ('wmic datafile where "Name='c:\\Windows\\System32\\notepad.exe'" get version /format:list') do set "npver=%%a"
echo %npver%
pause

EDIT : Get_File_Version.bat

Here is another code in batch more elaborated to get file version of any Application from file list using WMIC.

@echo off
Mode 75,3 & color 9E
Title Get File Version of any Application from file list using WMIC by Hackoo
::******************************************************************************
REM Just Change this Variable in your case and the script will do the rest (-_°)
Set "RootFolder=%ProgramData%"
::******************************************************************************
:Main
@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 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
::*******************************************************************
: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
REM Thanks to ImDeepWithWindows for this nice trick of 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
::*******************************************************************


来源:https://stackoverflow.com/questions/58942899/how-to-get-file-version-in-variable-using-cmd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!