Arnaud
Arnaud

Reputation: 31

Execute a script depending of the screen resolution

I tried to find the answer on some other threads, but i think that what i'm trying to do is a bit "specific". I'm not enough good with batch to adapt/concatenate parts of scripts i've found as well...

So, i'm trying to execute a command, depending of the running screen resolution. The context is the following;

The command executed at the logon is placing the shortcuts on the desktop specifically, but it's not the same placement between the resolutions...

the idea is to define a variable, which is the answer of a wmic desktopmonitor get screenheight, screenwidth request. Then if the output contains 1080, so execute this cmd, else if it contains 720, execute another one, etc...

thats the cmd i use for win7 (working);

for /f "tokens=1-2 delims= " %%r in ('wmic desktopmonitor get screenheight^,  screenwidth ^| findstr "1"') do set current_res=%%sx%%r
if "%current_res%" == "1920x1080" C:\Windows\kiosque\desktopok.exe /load /silent c:\windows\kiosque\dispo_icones_1080p.dok

i need to do the same with win10 with the wmic path Win32_VideoController get VideoModeDescription, but i didn't found how to define the output of this request properly as a variable...

Upvotes: 3

Views: 851

Answers (2)

Compo
Compo

Reputation: 38579

Because Win32_VideoController has been tested as working on my Windows 7 and Windows 10 systems, here are some Win32_VideoController examples:

Retrieving the horizontal resolution, as intimated in your question as the determining factor:

For /F "Delims=" %%A In (
    'WMIC Path Win32_VideoController Get CurrentHorizontalResolution'
) Do For %%B In (%%A) Do Set "ResW=%%B"

Likewise if you wanted to check only the vertical resolution:

For /F "Delims=" %%A In (
    'WMIC Path Win32_VideoController Get CurrentVerticalResolution'
) Do For %%B In (%%A) Do Set "ResH=%%B"

And if you wanted the resolution in WxH format:

@Echo Off
Set "WP=Path Win32_VideoController"
Set "WV=CurrentHorizontalResolution,CurrentVerticalResolution"

For /F "Skip=1 Tokens=*" %%A In ('"WMIC %WP% Get %WV%"'
) Do For /F "Tokens=1-2" %%B In ("%%A") Do Set ScRes=%%Bx%%C
Echo=%ScRes%
Pause

If you wanted a version which accounts for both DesktopMonitor and Win32_VideoController, then perhaps this will do, (from Vista onwards):

@Echo Off
Set "OV="
For /F "EOL=V" %%A In ('WMIc OS Get Version 2^>Nul'
) Do For /F "Tokens=1-2 Delims=." %%B In ("%%A") Do Set /A "OV=%%B%%C"
If Not Defined OV Exit /B
Set "ScRes=%%Cx%%B" & Set "WP=DesktopMonitor"
Set "WV=ScreenHeight,ScreenWidth"
If %OV% GEq 61 (Set "WP=Path Win32_VideoController" & Set "ScRes=%%Bx%%C"
    Set "WV=CurrentHorizontalResolution,CurrentVerticalResolution")
For /F "Skip=1 Tokens=*" %%A In ('"WMIC %WP% Get %WV%"'
) Do For /F "Tokens=1-2" %%B In ("%%A") Do Set ScRes=%ScRes%
Echo=%ScRes%
Pause

I have left line 8 as GEq 61 for versions of at least Windows 7, because as I've stated, it works on my Windows 7 version.
You could however change that to read Gtr 61 for Windows 8/Server 2012 and above, or even Gtr 63 if you want to limit it to anything above Windows 8.1/Server 2012 R2

Upvotes: 2

npocmaka
npocmaka

Reputation: 57252

You need different wmic queries depending on the windows version.Here's a resolution getter that depends on the version:

@echo off
::https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
setlocal
for /f "tokens=4,5 delims=. " %%a in ('ver') do set "version=%%a%%b"


if version lss 62 (
    ::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%
::if "%x%x%y%" == "1920x1080" C:\Windows\kiosque\desktopok.exe /load /silent c:\windows\kiosque\dispo_icones_1080p.dok

endlocal

For windows 7 or earlier you need desktopmonitor class for the newer windows versions you need Win32_VideoController .you can try with dxdiag too:

@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

Upvotes: 2

Related Questions