我有一个BAT脚本,应该读取当前屏幕分辨率的变量。我使用wmic desktopmonitor get screenwidth /value命令获得屏幕分辨率(对于宽度,高度是相同的)。
示例输出:
C:\Users\Pietu1998>wmic desktopmonitor get screenwidth /value
ScreenWidth=1920
ScreenWidth=
C:\Users\Pietu1998>我有两个显示器,所以它只显示正在使用的显示器的分辨率。
我尝试使用for循环跳过前两行空行,然后读取数据。
set screenwidth=
for /F "skip=2 tokens=*" %%F in ('wmic desktopmonitor get screenwidth /value') do (
if "%screenwidth%" equ "" (
echo line: %%F
set screenwidth=%%F
echo var: %screenwidth%
set screenwidth=%screenwidth:~12%
)
)我得到的输出是正确的,因为行是由第一个echo打印的,但是由于某种原因,第二个echo没有输出任何输出。变量中不放这一行。
我在这里错过了什么?我已经在谷歌上搜索了两个小时。
更新:我找到了一种使用findstr和临时文件的方法。
wmic desktopmonitor get screenwidth /value | findstr "ScreenWidth=." > %temp%\tmp
set /P screenwidth=< %temp%\tmp
del %temp%\tmp发布于 2014-05-18 17:48:48
echo var: %screenwidth% <--如果没有一些特殊的技术,比如延迟扩展,就不能在循环中设置和使用变量。
这个能满足你的需要吗?
@echo off
set "screenwidth="
for /F "tokens=2 delims==" %%F in ('wmic desktopmonitor get screenwidth /value') do (
if not defined screenwidth set screenwidth=%%F
)
pausehttps://stackoverflow.com/questions/23724982
复制相似问题