在批量编程中使用FOR

时间:2015-12-09 07:21:45

标签: batch-file

在批处理脚本中使用FOR时遇到问题。

我的批处理文件包含以下内容:

Echo off
set max_page=2
for /l %%i IN (1,1,%max_page%) do (
echo.
echo  Page %%i off %max_page%
echo  ========================================================
echo page %%i

:find_server
set "result=true"
echo page %%i
wget -S --spider -t 1 http://portal/ 2> check_portal.txt
echo page %%i
for /f "skip=1 tokens=4" %%b in ('findstr /L "Connecting" check_portal.txt') do set status=%%b
echo page %%i
if "%status%"=="failed:" set result=false
echo page %%i
if "%result%"=="false" (
    goto :find_server
)else goto :server_connected

:server_connected
for /f "skip=3 tokens=2 delims=/" %%c in ('findstr /L "Location: " check_portal.txt') do set server_portal_upd=%%c
echo page %%i
)
pause

结果:

 Page 1 off 2
 ========================================================
page 1
page 1
page 1
page 1
page 1
page %i
Press any key to continue . . .

但如果删除下面的行,则脚本会成功运行

:find_server
    set "result=true"
    echo page %%i
    wget -S --spider -t 1 http://portal/ 2> check_portal.txt
    echo page %%i
    for /f "skip=1 tokens=4" %%b in ('findstr /L "Connecting" check_portal.txt') do set status=%%b
    echo page %%i
    if "%status%"=="failed:" set result=false
    echo page %%i
    if "%result%"=="false" (
        goto :find_server
    )else goto :server_connected

    :server_connected
    for /f "skip=3 tokens=2 delims=/" %%c in ('findstr /L "Location: " check_portal.txt') do set server_portal_upd=%%c

为什么Line 8显示page %i而非page 1? 哪里出错了?请纠正我。 非常感谢你的sugesstion

1 个答案:

答案 0 :(得分:2)

标签:find_server等终止for循环。尝试重组以使用call :subroutineneame

BTW - else关键字的 两侧必须有空格。