Ivan C
Ivan C

Reputation: 35

Get screen resolution as a variable in cmd

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?

Upvotes: 2

Views: 5900

Answers (3)

Stephan
Stephan

Reputation: 56180

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%

Upvotes: 2

ARF
ARF

Reputation: 7694

@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

Upvotes: 4

sms247
sms247

Reputation: 4504

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

Upvotes: 0

Related Questions