destructive_code
destructive_code

Reputation: 1

Batch file Programming Help required

I am writing a batch file program that gets a username and password from the user and checks them by comparing them with two files, username.dat and password.dat.

The username is stored in %username% and password in %password%. What I'd like to know is, how do I check these variables against the files username.dat and password.dat?

I've tried using this:

echo %username% >U.dat
echo %Password% >P.dat
COMP U.dat Username.dat

But what value to check? I mean in C or C++ we can check for a return value, but what about this COMP?

Upvotes: 0

Views: 146

Answers (1)

dbenham
dbenham

Reputation: 130809

Storing a username and password in files seems like a bad idea to me. But assuming you are going to do it any way...

There is no need to echo your variables to temporary files prior to doing a comparison. There are multiple ways to do your comparison more directly. Here is one method. I'm using delayed expansion so that it supports special characters like & | etc in the password (or username).

setlocal enableDelayedExpansion
set userPassOK=1
findstr /x /c:"!username!" Username.dat || set "userPassOK="
findstr /x /c:"!password!" Password.dat || set "userPassOK="
if defined userPassOK (echo username and password passed) else echo Invalid username/password

Upvotes: 2

Related Questions