Get screen resolution as a variable in cmd

2019-07-16 12:01发布

I need a script to copy a specific image depending on the screen resolution being used. so far I've found that wmic desktopmonitor get screenheight is giving me the appropriate output but i'm having trouble parsing it to a useable variable the problem is that the output is on three lines and I only need the information from the second one.

Can anyone help?

3条回答
Explosion°爆炸
2楼-- · 2019-07-16 12:18

You can even get more parameters with one single wmic:

for /f %%i in ('wmic desktopmonitor get screenheight^,screenwidth /value ^| find "="') do set "%%f"
echo your screen is %screenwidth% * %screenheight% pixels

If you need to have your own variablenames, it's a bit more complicated:

for /f "tokens=1,2 delims==" %%i in ('wmic desktopmonitor get screenheight^,screenwidth /value ^| find "="') do (
  if "%%i"=="ScreenHeight" set height=%%j
  if "%%i"=="ScreenWidth" set width=%%j
)
echo your screen is %width% * %height% pixels

if you need only one value:

for /f "tokens=2 delims==" %%i in ('wmic desktopmonitor get screenheight /value ^| find "="') do set height=%%i
echo %height%
查看更多
地球回转人心会变
3楼-- · 2019-07-16 12:26
for /f "tokens=*" %%f in ('wmic desktopmonitor get screenheight /value ^| find "="') do set "%%f"
echo %size%
pause
查看更多
女痞
4楼-- · 2019-07-16 12:32

@Stephan's answer modified to be compatible with all windows versions (including Windows 8):

setlocal
for /f %%i in ('wmic path Win32_VideoController get CurrentHorizontalResolution^,CurrentVerticalResolution /value ^| find "="') do set "%%i"
echo your screen is %CurrentHorizontalResolution% * %CurrentVerticalResolution% pixels
查看更多
登录 后发表回答