I can display the video graphics information of a basic laptop, but cannot succeed if the laptop has two video chips, e.g. Intel and Nvidia. If I run my command line in command prompt window:
wmic.exe path Win32_VideoController get CurrentHorizontalResolution^, CurrentVerticalResolution^, Name^ /^format:value
I get:
CurrentHorizontalResolution=
CurrentVerticalResolution=
Name=NVIDIA Quadro M1000M
CurrentHorizontalResolution=1920
CurrentVerticalResolution=1080
Name=Intel(R) HD Graphics 530
My code is:
set cmdlin="%SystemRoot%\System32\wbem\wmic.exe path Win32_VideoController get CurrentHorizontalResolution^,CurrentVerticalResolution^,Name^/format:value"
for /F "tokens=2-4 delims== skip=2" %%i in ('%cmdlin%') do (
for /F %%j in ("%%i") do (
set VresH=%%i
set VresV=%%j
set Vname=%%k
ECHO Res.H: %VresH% - Res.V:%VresV% - Graphics: %Vname%
)
)
I have tried various echoing without success. I can see the values fed in the variables with echo on
but not displayed...
Can somebody help to solve this and explain my errors, specially how to deal if the requested fields contains several items separated with spaces?
There can be used the following batch file code:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
if exist %SystemRoot%\System32\wbem\wmic.exe goto OutputVideoData
echo ERROR: The WMI command-line utility wmic.exe is not installed.
echo(
echo There should not be used anymore batch files with WMIC on this computer.
echo(
echo There could be installed WMIC via Settings ^> Apps ^> Optional features.
echo(
pause
exit /B
:OutputVideoData
for /F "tokens=1* delims==" %%G in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_VideoController GET CurrentHorizontalResolution^,CurrentVerticalResolution^,Name /VALUE 2^>nul') do (
if "%%G" == "CurrentHorizontalResolution" (
set "VresH=none"
set /A VresH=%%H 2>nul
) else if "%%G" == "CurrentVerticalResolution" (
set "VresV=none"
set /A VresV=%%H 2>nul
) else if "%%G" == "Name" (
for /F delims^=^ eol^= %%I in ("%%H") do (
set "Vname=%%I"
setlocal EnableDelayedExpansion
echo Res.H: !VresH! - Res.V: !VresV! - Graphics: !Vname!
endlocal
)
)
)
endlocal
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
exit /?
for /?
goto /?
if /?
pause /?
set /?
setlocal /?
wmic /?
wmic path /?
wmic path Win32_VideoController /?
wmic path Win32_VideoController get /?
Read also:
The last referenced answer explains most of the code of the FOR loop capturing and processing the output of WMIC which is not easy to understand.
The FOR /F options "tokens=2-4 delims== skip=2"
do not make sense on using WMIC with /format:value
or shorter just /VALUE
because of there is not output any line with four strings of interest separated by at least three equal signs. All three data on one line would be output by WMIC on omitting the option /VALUE
respectively the option /format:value
but that makes it very hard to process the line if nothing is output for CurrentHorizontalResolution
and for CurrentVerticalResolution
with the missing values replaced by spaces. The line contains just lots of spaces left to the name of the video controller if there are no values for CurrentHorizontalResolution
and CurrentVerticalResolution
.
set /A VresH=%%H
and set /A VresV=%%H
result in output of the error message Missing operand.
to handle STDERR which is redirected with 2>nul
to the device NUL to suppress it if there is no CurrentHorizontalResolution
and no CurrentVerticalResolution
for the video controller. The current value of the environment variables VresH
and VresV
are not changed in this case which is the reason for initializing them with the string value none
.
The main FOR command line can be also:
for /F "tokens=1* delims==" %%G in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_VideoController GET CurrentHorizontalResolution^,CurrentVerticalResolution^,Name /VALUE 2^>nul ^| %SystemRoot%\System32\find.exe "="') do (
FIND filters out the empty lines output by WMIC resulting in less loop iterations.
FIND outputs the data in ASCII with just one byte per character instead of UTF-16 LE. But each line with data is terminated with carriage return + carriage return + line-feed (0D 0D 0A) instead of just carriage return + line-feed (0D 0A). cmd.exe
started in background is responsible for passing the Unicode output of WMIC in ASCII with an extra carriage return at end of each line to FIND. The two set /A
instead of just set
and the inner FOR loop for removing the unwanted carriage return are still necessary with that small enhancement.