赞
踩
bat脚本中命令执行的结果赋值给变量
rem 查找ip地址举例
for /f "delims=" %%i in ( 'ipconfig /all^| find /i "ipv4" ') do set MYIP=%%i & echo %MYIP%
rem result.txt的文件内容:
rem [INFO] APP DEMO ................................. SUCCESS [ 1.111 s]
rem 查找命令:在文件内容中查找字符串 ".... FAIL " 或者 ".... SKIP " 或者 ".... ERR ",查找结果输出到tmp.txt
set MYFILE="%CD%\result.txt"
findstr "\.\.\.\.\ FAIL \.\.\.\.\ SKIP \.\.\.\.\ ERR " %MYFILE% > tmp.txt
rem 查找结果写文件以后,再处理文件中的结果
for /f "tokens=* delims=" %%i in (tmp.txt) do echo %%i
for /f "tokens=* delims=" %%1 in (tmp.txt) do echo %%1
for /f "tokens=1,2 delims=...." %%a in (tmp.txt) do echo %%a %%b
for /f "tokens=1,2,3 delims=...." %%1 in (tmp.txt) do echo %%2 %%3
for /f "tokens=1-3 delims=...." %%1 in (tmp.txt) do echo %%2 %%3
for /f "tokens=* delims=...." %%1 in (tmp.txt) do echo %%1
rem 也可以直接将bat命令执行的结果字符串赋值给变量,省去了中间的写文件和读文件处理的过程
for /f "tokens=1-2 delims=...." %%1 in ( 'findstr "\.\.\.\.\ FAIL \.\.\.\.\ SKIP " %MYFILE% ' ) do (set STATUS=%%2 & echo %%1 , %%2 & echo ======)
rem IF "%STATUS%" EQU "" echo " 查找失败结果为空,未找到失败信息"
IF "%STATUS%" EQU "" (
echo " 无失败信息,继续处理"
) else (
echo "有失败信息,跳到结束"
GOTO END
)
echo ##########
echo -------- %STATUS%
echo -------- %MYIP%
set MYIP=
set STATUS=
set MYFILE=
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。