Get screen resolution as a variable in cmd

2019-07-16 12:18发布

问题:

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?

回答1:

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%


回答2:

@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


回答3:

for /f "tokens=*" %%f in ('wmic desktopmonitor get screenheight /value ^| find "="') do set "%%f"
echo %size%
pause