我使用wmic获取存储在变量中的EXE版本。 当我从CMD执行(我以Admin身份运行)时,它工作正常,但是在.cmd或.bat文件中执行(即使以admin身份运行),也不会填充变量。
这直接在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"
创建.cmd文件时,我将%符号加倍。
答案 0 :(得分:1)
您忘记了使用命令echo %npver%
使用命令行:
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%
对于批处理文件,您应该通过添加另一个%
来逃避符号百分号%%
并且您的代码现在应该可以使用这样的批处理文件:
批处理文件:
@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
编辑:Get_File_Version.bat
这是另一个批处理代码,详细说明了可以使用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
::*******************************************************************