根据屏幕分辨率执行脚本。

3
我试图在其他帖子中找到答案,但我认为我要做的有点“特定”。我不太懂批处理语言,无法将我发现的脚本部分进行调整/合并...

因此,我正在尝试根据运行屏幕分辨率执行命令。背景如下:

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

想法是定义一个变量,该变量是请求的答案。然后,如果输出包含1080,则执行此cmd,否则,如果它包含720,则执行另一个cmd等等...

这是我在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 path Win32_VideoController get VideoModeDescription 在Win10上执行相同的操作,但我找不到如何将此请求的输出正确定义为变量...


2
在我的Win10上似乎可以工作,尽管我有两个显示器,所以值设置为最后一个,这是预期的。你具体遇到了什么问题? - Magoo
2
wmic path Win32_VideoController VideoModeDescription 的输出是 1920 x 1080 x 4294967296 colors。现在你有了3个标记。所以请修改你的代码,使用3个标记,你的变量将会是 %%r、%%s 和 %%t。 - Squashman
2
顺便提一下,这两个都在我的Windows 107上工作,但是在Windows 10上,ScreenHeightScreenWidth都没有值。 Win32_VideoController 在两个系统上都可以完美地工作,因此基于这一点,我更喜欢使用它。使用 Win32_VideoController,您应该检索 CurrentHorizontalResolution 和/或 CurrentVerticalResolution 并根据需要使用它们。由于实际上只是在现有命令和新命令之间替换几个字符串,所以您的问题到底是什么? - Compo
感谢大家的回答。正如Compo所说,“wmic desktopmonitor”查询在Win10上返回空值。但我没有找到使用“wmic path Win32…”查询执行相同操作的方法,我认为我没有正确使用令牌。然而,@npocmaka提供的脚本完全适合我。 - Arnaud
2个回答

2

因为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"

如果您想以宽x高的格式获得分辨率:

@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开始,这个可能会有所帮助:(从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

我将第8行代码留作GEq 61,适用于至少Windows 7版本,因为如我所说,它在我的Windows 7版本上运行良好。
但是,您可以将其更改为Gtr 61,以适用于Windows 8/Server 2012及以上版本,甚至可以将其限制为高于Windows 8.1/Server 2012 R2的任何版本Gtr 63


谢谢!你的帮助非常宝贵。我选择了@npocmaka下面的wmic查询,因为对我来说非常清晰!但是你的答案看起来也很相似,我认为它也可以工作。 - Arnaud

2

根据Windows版本,您需要不同的wmic查询。以下是一个取决于版本的分辨率获取器:

@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类。对于更新的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

1
谢谢,你输入的第一个脚本非常适合我的情况。它运行得很好! - Arnaud

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接