l1ght3d
l1ght3d

Reputation: 67

BATCH - Get display resolution from windows command line and set variable

@echo off
set h=wmic desktopmonitor, get screenheight
set w=wmic desktopmonitor, get screenwidth
echo %h%
echo %w%
pause

Instead of getting -

1600 2560

I get -

echo wmic desktopmonitor, get screenwidth

echo wmic desktopmonitor, get screenheight

I want this batch script to be able to get my display resolution size and set it in height and width variable and able to be echo'd in number.
But it's not seem to be working.

Upvotes: 5

Views: 19933

Answers (5)

Kendavros
Kendavros

Reputation: 1

A wmic 'all in one' solution using one call (200ms versus 350ms of jan 2, 2019)

@echo off
  set "zzTmp=desktopmonitor get ScreenWidth^,ScreenHeight"
  for /f "tokens=4,5 delims=. " %%a in ('ver') do set "version=%%a%%b"
  echo Version: %version%
  if version GTR 62 set "zzTmp=path Win32_VideoController get CurrentHorizontalResolution^,CurrentVerticalResolution"

  for /f %%s in ('wmic %zzTmp% /value ^|find "="') do set "zZ%%s"1>nul 2>nul
  if defined zZCurrentHorizontalResolution set "zZScreenWidth=%zZCurrentHorizontalResolution%"
  if defined zZCurrentVerticalResolution set "zZScreenHeight=%zZCurrentVerticalResolution%"
  if not defined zZScreenWidth set "zZScreenWidth=1024" & set "zZScreenHeight=768"
  set /a zzSsW=%zZScreenWidth%
  set /a zzSsH=%zZScreenHeight%
  set "zzTmp="
  set "zZScreenWidth="
  set "zZScreenHeight="
  set "zZCurrentHorizontalResolution="
  set "zZCurrentVerticalResolution="

  echo. Resolution : [%zzSsW%]x[%zzSsH%]%zz%

Upvotes: 0

Io-oI
Io-oI

Reputation: 2565

I am curious if this code works or not on another Windows (tested on Windows 10 pro x64):

:: in command line ::
@for /f delims^= %I in ('wmic.exe path Win32_VideoController get CurrentHorizontalResolution^,CurrentVerticalResolution^|findstr /b [0-9]')do @for /f tokens^=1-2 %a in ('echo\%I')do @set "h=%a" && @set "w=%b" && call echo=%h%x%w%

:: in bat/cmd file ::
@echo off && setlocal enabledelayedexpansion

for /f delims^= %%I in ('
wmic.exe path Win32_VideoController get CurrentHorizontalResolution^,CurrentVerticalResolution^|findstr /b [0-9]
')do for /f tokens^=1-2 %%a in ('echo\%%I')do set "h=%%a" && set "w=%%b" && call echo=!h!x!w!

  • Outputs:

1920x1080

  • The variable %h% and %w% return in echo=%h% %w% 1920 1080

Upvotes: 0

George H. Compton IV
George H. Compton IV

Reputation: 11

 @echo off
 title  Detect Screen Resolution
 REM Detect Screen Resolution in Windows 7 - 10 - 32/64 bit compatible.
 REM You must have the proper graphics driver installed in your system.

 set loop#=2
 set /a loop=1     

 for /f "tokens=*" %%c in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Hardware Profiles\UnitedVideo\CONTROL\VIDEO"  ^| find "{"') do @set "Resolution-path=%%c" && goto next
 for /f "tokens=*" %%c in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\UnitedVideo\CONTROL\VIDEO"  ^| find "{"') do @set "Resolution-path=%%c" && echo win 10 && goto next

      :next   

 for /f "tokens=*" %%c in ('reg query "%Resolution-path%\0000"  /v "DefaultSettings.XResolution"  ^| find "0"') do @set "XResolution-HEX=%%c" 
 for /f "tokens=*" %%c in ('reg query "%Resolution-path%\0000"  /v "DefaultSettings.YResolution"  ^| find "0"') do @set "YResolution-HEX=%%c" 

 if %XResolution-HEX:~44% == 0x400 goto not1
 if 0 == %XResolution-HEX:~44,-4%  goto Y1
 goto not1

      :Y1
   ver | find "6.1" > nul 
      if %ERRORLEVEL% == 0 echo %Resolution-path:~84% win 7 && goto next2
  ver | find "6.2" > nul  
      if %ERRORLEVEL% == 0 echo %Resolution-path:~84% win 8 && goto next2    
 echo %Resolution-path:~74% win 10  
       :next2
 echo %XResolution-HEX:~44%
 echo %YResolution-HEX:~44%
 if %XResolution-HEX:~44% == 0x780 echo . && if %YResolution-HEX:~44% == 0x438  set screen-resolution=1920x1080
 if %XResolution-HEX:~44% == 0x6f0 echo . && if %YResolution-HEX:~44% == 0x3e8  set screen-resolution=1776x1000 
 if %XResolution-HEX:~44% == 0x690 echo . && if %YResolution-HEX:~44% == 0x41a  set screen-resolution=1680x1050 
 if %XResolution-HEX:~44% == 0x640 echo . && if %YResolution-HEX:~44% == 0x384  set screen-resolution=1600x900 
 if %XResolution-HEX:~44% == 0x556 echo . && if %YResolution-HEX:~44% == 0x300  set screen-resolution=1366x768 
 if %XResolution-HEX:~44% == 0x550 echo . && if %YResolution-HEX:~44% == 0x300  set screen-resolution=1360x768
 if %XResolution-HEX:~44% == 0x500 echo . && if %YResolution-HEX:~44% == 0x400  set screen-resolution=1280x1024
 if %XResolution-HEX:~44% == 0x500 echo . && if %YResolution-HEX:~44% == 0x3c0  set screen-resolution=1280x960
 if %XResolution-HEX:~44% == 0x500 echo . && if %YResolution-HEX:~44% == 0x300  set screen-resolution=1280x768
 if %XResolution-HEX:~44% == 0x500 echo . && if %YResolution-HEX:~44% == 0x2d0  set screen-resolution=1280x720
 if %XResolution-HEX:~44% == 0x480 echo . && if %YResolution-HEX:~44% == 0x288  set screen-resolution=1152x648
 if %XResolution-HEX:~44% == 0x400 echo . && if %YResolution-HEX:~44% == 0x300  set screen-resolution=1024x768
 if %XResolution-HEX:~44% == 0x320 echo . && if %YResolution-HEX:~44% == 0x258  set screen-resolution=800x600
 setx screen-resolution "%screen-resolution%"
 echo Detected screen resolution.
 echo %screen-resolution%
 echo.
 echo.
 pause
 exit
      :not1
 if %loop% == %loop#% goto not     
 set /a loop=loop+1     
 for /f "tokens=*" %%c in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Hardware Profiles\UnitedVideo\CONTROL\VIDEO"  ^| find "{"') do @set "Resolution-path=%%c" 
 for /f "tokens=*" %%c in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\UnitedVideo\CONTROL\VIDEO"  ^| find "{"') do @set "Resolution-path=%%c" && echo win 10 
       goto next

      :not
 setlocal
 color 4f
 cd /d %~dp0     
 echo  failed exiting
 timeout /t 05  
 exit 

Upvotes: 1

npocmaka
npocmaka

Reputation: 57322

with desktopmonitor you can get only dpi.For pixel resolution you need Win32_VideoController :

@echo off

for /f "delims=" %%# in  ('"wmic path Win32_VideoController  get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"') do (
  set "%%#">nul
)

echo %CurrentHorizontalResolution%
echo %CurrentVerticalResolution%

If you want I can add also a dpi resolution getter? And if have more than one monitors I'll have to modify the script...

Another way that will allow you to get resolution of more monitors is to use DxDiag (though it will create a temp file and will be slower ):

With dxdiag :

@echo off

del ~.txt /q /f >nul 2>nul
start "" /w dxdiag /t ~
setlocal enableDelayedExpansion
set currmon=1 
for /f "tokens=2 delims=:" %%a in ('find "Current Mode:" ~.txt') do (
    echo Monitor !currmon! : %%a
    set /a currmon=currmon+1

)
endlocal
del ~.txt /q /f >nul 2>nul

this will print the resolutions of all monitors.

EDIT:

A wmic script that will detect the version of windows and will use different wmi classes if needed:

@echo off

setlocal
for /f "tokens=4,5 delims=. " %%a in ('ver') do set "version=%%a%%b"


if version lss 62 (
    ::set "wmic_query=wmic desktopmonitor get screenheight, screenwidth /format:value"
    for /f "tokens=* delims=" %%@ in ('wmic desktopmonitor get screenwidth /format:value') do (
        for /f "tokens=2 delims==" %%# in ("%%@") do set "x=%%#"
    )
    for /f "tokens=* delims=" %%@ in ('wmic desktopmonitor get screenheight /format:value') do (
        for /f "tokens=2 delims==" %%# in ("%%@") do set "y=%%#"
    )

) else (
    ::wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,CurrentHorizontalResolution /format:value
    for /f "tokens=* delims=" %%@ in ('wmic path Win32_VideoController get CurrentHorizontalResolution  /format:value') do (
        for /f "tokens=2 delims==" %%# in ("%%@") do set "x=%%#"
    )
    for /f "tokens=* delims=" %%@ in ('wmic path Win32_VideoController get CurrentVerticalResolution /format:value') do (
        for /f "tokens=2 delims==" %%# in ("%%@") do set "y=%%#"
    )

)

echo Resolution %x%x%y%

endlocal

Upvotes: 8

nicely done :-) to prevent the empty set commands i propose this little addition using find:

FOR /f "delims=" %%a IN ('%comspec% /c "wmic desktopmonitor get ScreenWidth,ScreenHeight" /value ^| find "="') DO (SET %%a)

Upvotes: 0

Related Questions