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

ⅰ亾dé卋堺 提交于 2019-12-21 17:36:38

问题


@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.


回答1:


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



回答2:


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)



回答3:


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 desktopmonitor get ScreenHeight,ScreenWidth /format:table|find "0""')do for /f tokens^=1^,2 %a in ('echo=%I')do set "h=%b" & set "w=%a"

:: in bat/cmd file ::
for /f delims^= %%I in ('"wmic desktopmonitor get screenHeight,screenWidth /format:table|find "0""')do for /f tokens^=1^,2 %%a in ('echo=%%I')do set "h=%%b" & set "w=%%a"


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

    1920 1080



来源:https://stackoverflow.com/questions/25594848/batch-get-display-resolution-from-windows-command-line-and-set-variable

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