views:

290

answers:

2

I like to have a batch file which checks if an entered text in a .txt file is the same.

Something like this...:

@echo off
Set pass=
set /p pass=Enter your password: 
......
......

the .txt file is pass.txt and it should look something like this:

p2342ddd3

So what i want it to do, that an user have to type in the text from the pass.txt file (not looking at it obviously) and that the batch file checks if it is similar with the text from the pass.txt file.

+2  A: 

This will require a combination of a for loop and simple if:

@echo off
:begin
set pass=
set /p pass=Enter your password: 
if {%pass%}=={} goto :begin
set authenticated=
for /f "tokens=*" %%a in (pass.txt) do (
    if {%%a}=={%pass%} set authenticated=true
)

if not defined authenticated (echo Invalid password & goto :begin)
exit /b 0
esac
A: 

You can use the built-in command FINDSTR to match the password in the password file:

@echo off
set pass=
set /p pass=Enter your password:

findstr /B /E /M %pass% pass.txt > nul
If %ERRORLEVEL% EQU 0 echo Password matched!

Options /B and /E are for ensuring that the whole password is matched and no partial matching takes place. E.g. 42 is contained in p2342ddd3, but should not result in a match.

Options /M and the redirection to nul is to ensure the password does not leak out.

FINDSTR sets variable ERRORLEVEL to 0 if an item is found (password match) and to a value greater than 0 if an item is not found.

Peter Mortensen