根据屏幕分辨率执行脚本

Execute a script depending of the screen resolution

我试图在其他一些线程上找到答案,但我认为我想做的有点 "specific"。我对 adapt/concatenate 部分脚本的批处理不够好...

因此,我正在尝试根据 运行 屏幕分辨率执行命令。 上下文如下;

登录时执行的命令专门将快捷方式放在桌面上,但分辨率之间的位置不同...

想法是定义一个变量,它是 wmic desktopmonitor get screenheight, screenwidth 请求的答案。然后如果输出包含 1080,则执行此命令,否则如果包含 720,则执行另一个,等等...

这就是我用于 win7 的 cmd(工作);

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

我需要用 wmic 路径 Win32_VideoController get VideoModeDescription 对 win10 做同样的事情,但我没有找到如何正确定义此请求的输出作为变量...

因为 Win32_VideoController 已经在我的 Windows 7Windows 10 系统上进行了测试,这里有一些 Win32_VideoController 示例:

检索水平分辨率,在您的问题中作为决定因素暗示

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

同样,如果您只想检查垂直分辨率:

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

如果您想要 WxH 格式的分辨率:

@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

如果您想要一个同时考虑 DesktopMonitor 和 Win32_VideoController 的版本,那么也许可以这样做,(从 Vista 开始):

@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

对于至少 Windows 7 的版本,我将 8 行保留为 GEq 61,因为正如我所说,它适用于我的 Windows 7 版本.
然而,您可以将其更改为 Gtr 61 for Windows 8/Server 2012 及更高版本,甚至 Gtr 63 如果您想将其限制为任何高于 Windows 8.1/Server 2012 R2

您需要不同的 wmic 查询,具体取决于 windows version.Here 的分辨率 getter 取决于版本:

@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

对于 windows 7 或更早版本,您需要 desktopmonitor class 对于较新的 windows 版本,您需要 Win32_VideoController。您可以尝试使用 dxdiag 也是:

@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