0

I encounter a problem when using FOR in a batch script.

My batch file contain bellow :

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

Result :

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

But if the line below is removed , the script runs successfully

: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

Why Line 8 to appear page %i not page 1? where is the mistake? Correct me please. Thank you very much for your sugesstion

1 Answer 1

2

Labels :find_server etc. terminate the for loop. Try restructuring to use call :subroutineneame

BTW - there must be a space on both sides of the else keyword.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.