Marcucciboy2
Marcucciboy2

Reputation: 3257

Command line argument to get monitor resolution only returns one monitor

I have a Windows 7 laptop connected to two extra monitors. In order to extend the taskbar to these additional monitors (Win7 does not natively have this ability, unlike Win10) I use a program called DualMonitor.exe.

My problem is that when I disconnect the laptop from these extra monitors, the programs managed by DualMonitor become hidden. To remedy this, I made a simple batch file to restart explorer and DualMonitor.

@echo off

taskkill /f /im dualmonitor.exe
ping localhost -n 1 > nul

taskkill /f /im explorer.exe
ping localhost -n 3 > nul

echo.
echo restarting explorer

start explorer.exe
ping localhost -n 6 > nul

echo restarting dual monitor
start "" "C:\Users\X\AppData\Local\Dual Monitor\DualMonitor.exe"

The batch above works fine, but I wanted to adjust this so that it ONLY opens DualMonitor in the case that extra monitors are connected. I did some research and found these commands which can be modified to count the number of additional monitors connected to the laptop:

wmic desktopmonitor  get screenwidth, screenheight

wmic path Win32_VideoController  get CurrentHorizontalResolution, CurrentVerticalResolution

...but for me both of these only return the resolution for the laptop's monitor despite being connected to two additional monitors. I don't have admin rights to this machine so I am unable to utilize dxdiag in my solution.


End Result

@echo off

taskkill /f /im dualmonitor.exe
ping localhost -n 1 > nul

taskkill /f /im explorer.exe
ping localhost -n 3 > nul

echo.
echo restarting explorer

start explorer.exe
ping localhost -n 3 > nul

for /F %%M in ('
    wmic path Win32_PnPEntity where "Service='monitor' and Status='OK'" get DeviceID /VALUE ^
        ^| find /C "="
') do set count=%%M

if %count% GTR 1 (
    echo restarting dual monitor.
    start "" "C:\Users\X\AppData\Local\Dual Monitor\DualMonitor.exe"
    ping localhost -n 4 > nul
)

echo.
echo all done.
ping localhost -n 2 > nul

Upvotes: 2

Views: 3359

Answers (2)

aschipfl
aschipfl

Reputation: 34989

To determine the number of connected monitors you could use the following code:

for /F %%M in ('
    wmic path Win32_PnPEntity where "Service='monitor' and Status='OK'" get DeviceID /VALUE ^
        ^| find /C "="
') do echo There are %%M monitors.

Refer to the article Win32_PnPEntity class for the WMI class Win32_PnPEntity.

Upvotes: 3

Stephan
Stephan

Reputation: 56238

use wmic like you did, but select one property only and count the occurences:

for /f %%a in ('wmic desktopmonitor get DeviceID /value ^| find /c "="') do set "monitors=%%a"
echo there are %monitors% monitors.

Upvotes: 0

Related Questions