batch for /f not working right?

- / -
January 15, 2009 at 07:55:15
Specs: windows xp/2k/nt, -
I think I'm beginning to be addicted to this place...

I have a program that is constantly on.

Program below is to ask user for login and check if it exists. If it does, it will ask for a work id and check if it exists. If neither case matches, the program will print an error.
Lines in u009_users.txt go "code1 Name Here" and so on.
Lines in kp_tyot.txt go "work1 work 1 explained" and so on.

now the prompt for ID should ONLY accept what is the first string of each line in u009_users.txt but no. It also accepts just pressing enter, it always accepts user replies consisting of one character and it appears to do a complete search in u009_users.txt because it will accept any
given name found. Also if one succesful login and work log is made and after that for example "k" is given, it will result that last accepted login is %ID% which is not acceptable.

I tried to use solution given to someone by IVO earlier but an error or halt always came up at some point. If not earlier, they started coming when one log was succesfully made and another, erroneous log was made.

Here is the old thread:
http://www.computing.net/answers/pr...

My script:

@echo off
setlocal EnableDelayedExpansion

:Start
set /P ID=Log in: 
type u009_users.txt | find "%ID% ">NUL
if errorlevel 1 goto NoUser
COLOR 0F
call :UserID %ID%
echo Logged in: %Name%

:SetWork
echo.
set /P WORK=Work ID: 
type kp_tyot.txt | find "%WORK%">NUL
if errorlevel 1 goto NoWork
echo log %ID% %WORK% %COMPUTERNAME% %DATE% %TIME% >> %ID%.log
call :workID %WORK%

:PrintOk
echo Logged: %WORK%
call :Line
goto Start

:userID
for /f "tokens=1*" %%B in (u009_users.txt) do (
if [%1]==[%%B] (set Name=%%C & exit/b)
)
goto :EOF

:workID
for /f "tokens=1*" %%B in (kp_tyot.txt) do (
if [%1]==[%%B] (set workText=%%C & exit/b)
)
goto :EOF

:NoUser
echo.
COLOR 4F
@echo user ID not found.
call :Line
goto Start

:NoWork
echo.
COLOR 4F
@echo work ID not found.
call :Line
goto Start

:Line
echo.
-----------
echo.

:EOF

Thank you for co-operation!


See More: batch for /f not working right?

Report •


#1
January 15, 2009 at 13:14:31
To be honest I wasn't following your other threads so I'm not clear on the subject.

This might work:

for /f "tokens=1" %%B in (u009_users.txt) do if "%%B"=="!id!" goto break
goto nouser
:break

to replace:

type u009_users.txt | find "%ID% ">NUL
if errorlevel 1 goto NoUser

As for the problem with set /p, simply clear the variable before you ask for user input.

set work=
set /P WORK=Work ID:

The same method can be used for all your set /p statements.


Report •

#2
January 15, 2009 at 23:23:39
Oops, I made a simple mistake there, I wasn't even fetching the input data from the first string but from the whole file :P

Thank you!


Report •

Related Solutions


Ask Question