【问题标题】:Get screen resolution as a variable in cmd在cmd中获取屏幕分辨率作为变量
【发布时间】:2014-08-27 16:30:12
【问题描述】:

我需要一个脚本来根据所使用的屏幕分辨率复制特定图像。 到目前为止,我发现 wmic desktopmonitor get screenheight 给了我适当的输出,但我无法将其解析为可用的变量 问题是输出是三行,我只需要第二行的信息。

谁能帮忙?

【问题讨论】:

标签: windows batch-file cmd resolution


【解决方案1】:

@Stephan's answer修改为兼容所有windows版本(包括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

【讨论】:

    【解决方案2】:

    你甚至可以通过一个wmic获得更多参数:

    for /f %%i in ('wmic desktopmonitor get screenheight^,screenwidth /value ^| find "="') do set "%%f"
    echo your screen is %screenwidth% * %screenheight% pixels
    

    如果你需要有自己的变量名,那就有点复杂了:

    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
    

    如果你只需要一个值:

    for /f "tokens=2 delims==" %%i in ('wmic desktopmonitor get screenheight /value ^| find "="') do set height=%%i
    echo %height%
    

    【讨论】:

    • 完美,我只需要你最后一个 FOR 循环的一个值是理想的。
    • 一个额外的挑战,这在 Windows7 下工作,但在 8 下它不会产生任何输出.. 有什么想法可以替代 WMIC,这似乎被打破了?
    • 可能是权限问题。尝试wmic desktopmonitor get screenheight 如果您收到错误消息,请在新的 cmd 窗口中尝试相同的操作(“以管理员身份运行”)(是的,即使您是管理员)
    • 谢谢,但这是一个已知的但在 Win 8 下使用 WMIC
    【解决方案3】:
    for /f "tokens=*" %%f in ('wmic desktopmonitor get screenheight /value ^| find "="') do set "%%f"
    echo %size%
    pause
    

    【讨论】:

    • 您的for 将设置一个变量%screenheight%,而不是%size%
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 2012-02-15
    相关资源
    最近更新 更多