2

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?

Ivan C
  • 35
  • 2
  • 4
  • 1
    Show us the code you've tried and are having problems with. – aphoria Aug 27 '14 at 16:42
  • 1
    see here: http://stackoverflow.com/a/25340813/2152082 how to handle the ugly `wmic` line endings. Here: http://stackoverflow.com/a/23067935/2152082 is another way to do it. If you have problems adapting the code to your needs, edit your question with your "best try" – Stephan Aug 27 '14 at 17:24

3 Answers3

4

@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
Community
  • 1
  • 1
ARF
  • 7,420
  • 8
  • 45
  • 72
2

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%
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Perfect, I did only need the one value for your last FOR loop is ideal. – Ivan C Aug 28 '14 at 10:42
  • 1
    One additional challenge though, this works under Windows7 but under 8 it doesn't produce any output.. any ideas for an alternative to WMIC which seems to be broken? – Ivan C Aug 28 '14 at 10:43
  • 1
    Might be a permission problem. Try `wmic desktopmonitor get screenheight` If you get an errormessage, try the same in a new cmd-window ("run as administrator") (yes, even if you ARE adminstrator) – Stephan Aug 28 '14 at 11:11
  • 1
    Thanks, but this is a known but with WMIC under Win 8 – Ivan C Aug 28 '14 at 15:39
0
for /f "tokens=*" %%f in ('wmic desktopmonitor get screenheight /value ^| find "="') do set "%%f"
echo %size%
pause
Stephan
  • 53,940
  • 10
  • 58
  • 91
sms247
  • 4,404
  • 5
  • 35
  • 45