【发布时间】:2014-05-18 17:43:17
【问题描述】:
我有一个 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 没有输出任何内容。该行没有放在变量中。
我在这里缺少什么?我已经在谷歌上搜索了 2 个小时。
更新:我找到了一种使用findstr 和临时文件的方法。
wmic desktopmonitor get screenwidth /value | findstr "ScreenWidth=." > %temp%\tmp
set /P screenwidth=< %temp%\tmp
del %temp%\tmp
【问题讨论】:
标签: windows parsing batch-file output