0

I'm back with another question, this time it's about the FC console command. I'm making a chat program and my problem is:

I want people available to change password, but how do I check if the password is the same password as the last one?

EDIT: Thanks to Stephan for the answer!

if "%newpassword%"=="%oldpassword%" echo same password

EDIT2: The command above doesn't notice case-sensitive words. But I've already fixed that problem myself by using the command FC.

  • 1
    `if "%newpassword%"=="%oldpassword%" echo same password` – Stephan Jun 10 '14 at 15:33
  • to compare two files with fc: `fc a.txt b.txt && echo same || echo different` – Stephan Jun 10 '14 at 15:49
  • 1
    What do you mean by "doesn't notice difference between C and c"? By default, the `IF` command is case-sensitive, i.e. it would determine that `C`≠`c`. When you want it to *ignore* the case (to view `C` as equal to `c`), you can use the `/I` switch: `IF /I "%newpassword%"=="%oldpassword%" ...`. – Andriy M Jun 11 '14 at 09:21
  • @AndriyM Yeah, i mean that the command IF isn't case-sensitive, and the switch /I did not work for me. –  Jun 11 '14 at 10:41
  • Well, there's something wrong then, because `IF` *is* case-sensitive in Windows. (I guessed that `/I` wouldn't suit you because it makes `IF` *not* case-sensitive.) As an illustration, run this simple command, `IF "C" == "c" (ECHO Equal.) ELSE (ECHO Different.)` and you will (should) get `Different.` in the output. – Andriy M Jun 11 '14 at 11:18
  • Yeah, you're right. I did the same thing but it didn't work. Well the problem is already solved anyway... –  Jun 11 '14 at 17:35

2 Answers2

1

Not sure, what you want. fc is to compare files, you seem to want to compare two variables.

So here are both answers.

To compare variables:

if "%newpassword%"=="%oldpassword%" echo same password  

to compare two files with fc:

fc a.txt b.txt && echo same || echo different
foxidrive
  • 40,353
  • 10
  • 53
  • 68
Stephan
  • 53,940
  • 10
  • 58
  • 91
0

Assuming that there's a file containing the previously used password, this code would be helpful when used to read the contents of the file ..

For example.

File containing password =passwords.txt Place batch file at the directory containing the passwords.txt file

@echo off
Setlocal enabledelayedexpansion
: begin
For /f "tokens=*" %%a in (passwords.txt) do (
  Set /p np= enter new password :
  If "%%a"=="!np!" (
    Echo same password!
    Pause
    Goto begin
  ) else (
    Set "newpass=!np!"
    Echo !newpass!>passwords.txt
    Echo password changed
    Pause
    Goto begin
  )
)
Stephan
  • 53,940
  • 10
  • 58
  • 91
mordecai
  • 71
  • 4