Solved Compare user input to text file?

September 6, 2011 at 14:50:48
Specs: Windows XP
Hello all! I am trying to create a batch file that will take the users input and compare it to a text file to see if the variable matches any of the strings in the text. I will be using this for passwords. In the past I have had my batch files create a separate .crm file that contains that particular persons password. Is there anyway to be able to gather all passwords in one file and have the batch compare the user input to the list? Thank you in advance for any help.

See More: Compare user input to text file?

Report •


#1
September 6, 2011 at 17:36:11
✔ Best Answer
REM This first set statement prompts the user for the input

set /p choice=Enter the password:

REM Then it appends that input to one_file.txt

echo %choice% >> one_file.txt

REM Then this statement searches the file for that string

findstr %choice% one_file.txt

REM Errorlevel 1 means that the string exists
REM the double pipe "||" translates as "else" in batch
REM So the whole statement translates as:
REM If the string is found echo "password found!", if not (else) echo "password not found!"

if errorlevel 1 echo password found! || echo password not found!


Report •

#2
September 6, 2011 at 17:54:00
You can also use a different approach like this:

@ECHO OFF
SET /P PASS1=<PASS1.TXT
ECHO Enter passcode here:
:RETRY
SET PASS=
SET /P PASS=
IF %PASS%==%PASS1% GOTO HOME
GOTO RETRY
:HOME
(Your code here)

Simply make a text document called "pass1.txt" in the same place the batch file is in. Then open it and enter the desired passcode, save, and that will be the code required to get into the batch file.

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#3
September 7, 2011 at 08:12:37
UPDATE: I entered the code incorrect and jowahs response works great!! thank you again!


Thank you jowah and RMT2 for your fast responses. Unfortunately it does not seem to be working. It works when I only have one password in the file, but anytime I have a list of passwords it always counts the individual password entered as incorrect. Any thoughts on how to get it to work with a list?


Report •

Related Solutions

#4
September 7, 2011 at 08:28:14
Figured out a work around thanks to jowahs response. Here is the code I finished with

@ECHO OFF

:first
set /p choice=Enter pass:
findstr %choice% one_file.txt >

temp.txt
goto next

:second
set /p new=<temp.txt
if %choice%==%new% goto third
goto fourth

:third
echo it works
del temp.txt
goto end

:fourth
echo try again
goto second

:end
eof

I would also like to add a comment on my last post. There is a very high chance that the reason those responses did not work was USER ERROR. I am still pretty new to batch files so I may of missed something. Thank you again to Jowah and RMT2 for their responses.


Report •

#5
September 7, 2011 at 08:36:40
there were some errors on my previous code, here is the actual code:

@ECHO OFF

:first
set /p choice=Enter pass:
findstr %choice% one_file.txt > temp.txt
goto second

:second
set /p new=<temp.txt
if %choice%==%new% goto third
goto fourth

:third
echo it works
del temp.txt
goto end

:fourth
echo try again
goto second

:end
eof


Report •

#6
September 7, 2011 at 17:48:06
You will need to add a few more lines of code to add multiple passwords:

SET /P PASS2=<PASS2.TXT

Add the above line at the beginning, then put the following line right under the line IF %PASS%==%PASS1% GOTO HOME :

IF %PASS%==%PASS2% GOTO HOME

If you need to add even more, just keep on repeating the code except adding the variable PASS3, then comparing it to the entered password. Hope this helps!

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#7
September 9, 2011 at 08:37:07
Thank you RMT2 for all the help! You have made my life much easier. Thank you again

Report •

#8
September 9, 2011 at 15:36:45
You're welcome! :)

Mouse could not be found. Click OK to continue.
What to do now?


Report •


Ask Question