1.

Solve : Non-Looping For Loop?

Answer»

I am trying to make get a user to log in, so they can continue using the batch SCRIPT, but the login is failing in the second FOR loop...

Here is my code. List.txt is a FILE with about 30 lines, each different. The first word is a possible username, the second a possible password. No USERNAMES are the same.

Code: [Select]@echo off
setlocal enabledelayedexpansion
set fname=list.txt
:looplog
set num1=0
set num2=0
echo Enter your username.
set /p usr=Username^:
Echo Enter your password.
set /p pass=Password^:
pause
for /f "delims=" %%z in ('type %fname%') do (
set /a num1+=1
)
echo Lines^: %num1%
pause
for /f "tokens=1-2 delims= " %%a in ('type %fname%') do (
if "%%a"=="%usr" if "%%b"=="%pass%" goto cont
set /a num2+=1
if %num2%==%num1% cls & echo Username and/or password don't match. & goto looplog
)
:cont
echo HI!
pause
For some reason, the second FOR loop is not LOOPING...so it just keeps on going as if it didn't exist...I have no idea.
But, somebody once said,"If you can't fix it, Break it!"

So break up your code into pieces. See if thee is a way to get the second FOR thing in a BAT fioe by it self and see if it works by itself.

Asy you ALREADY know, batch files suffer from delayed execution. The first thins have not been done yet and it tries to do the next thing and the variables do not have the values. Breaking it into separate files might make it work. Or reveal what is wrong.Quote from: Geek-9pm on April 19, 2009, 05:50:49 PM

I have no idea.
But, somebody once said,"If you can't fix it, Break it!"

So break up your code into pieces. See if thee is a way to get the second FOR thing in a BAT fioe by it self and see if it works by itself.

Asy you already know, batch files suffer from delayed execution. The first thins have not been done yet and it tries to do the next thing and the variables do not have the values. Breaking it into separate files might make it work. Or reveal what is wrong.

The second FOR loop requires the first FOR loop (which does work) to calculate the amount of lines...so I can't really break it up. But I know what the problem is.
It just does the first line, and then does nothing else.
for /f "tokens=1-2 delims= " %%a in ('type %fname%') do (
if "%%a"=="%usr" if "%%b"=="%pass%" goto cont
set /a num2+=1
if %num2%==%num1% cls & echo Username and/or password don't match. & goto looplog
)
:cont
echo HI!
pauseCode: [Select]@echo off
setlocal enabledelayedexpansion
set fname=list.txt

:looplog
set num1=0
set num2=0
echo Enter your username.
set /p usr=Username:
Echo Enter your password.
set /p pass=Password:
pause
for /f "delims=" %%z in ('type %fname%') do (
set /a num1+=1
)
echo Lines: %num1%
pause
for /f "tokens=1-2 delims= " %%a in ('type %fname%') do (
if "%%a"=="%usr%" (
if "%%b"=="%pass%" (
goto :cont
)
)
set /a num2+=1
if !num2!==!num1! (
cls
echo Username and/or password don't match.
goto :looplog
)
)

:cont
echo HI^^!
pauseQuote from: Batcher on April 19, 2009, 09:19:33 PM
Code: [Select]@echo off
setlocal enabledelayedexpansion
set fname=list.txt

:looplog
set num1=0
set num2=0
echo Enter your username.
set /p usr=Username:
Echo Enter your password.
set /p pass=Password:
pause
for /f "delims=" %%z in ('type %fname%') do (
set /a num1+=1
)
echo Lines: %num1%
pause
for /f "tokens=1-2 delims= " %%a in ('type %fname%') do (
if "%%a"=="%usr%" (
if "%%b"=="%pass%" (
goto :cont
)
)
set /a num2+=1
if !num2!==!num1! (
cls
echo Username and/or password don't match.
goto :looplog
)
)

:cont
echo HI^^!
pause
Thanks!


Discussion

No Comment Found