Computer Hope

Software => Computer programming => Topic started by: Vbrawl on April 07, 2020, 11:50:21 AM

Title: Password In A Bat File
Post by: Vbrawl on April 07, 2020, 11:50:21 AM
Hello, i need help with the code:

@ECHO OFF
set tries=3
set password=PaSsWoRd
:checker
if %tries% EQU "0" goto :EXIT
Echo You have %tries% attempts!
color 5
set /P pass= Enter Password:
if /I %pass% EQU %password% (echo Secret_Message| clip
) else (set /a tries=%tries% -1 goto :checker)
EXIT
exit
_______________________________________ __________________________________
When i write a wrong password i get a "Missing operator." error. But when i write the right password it works.
I want the program to ask for a password then check it and if it's the right password then change the clipboard, if the password is wrong  the 1st time go back and put the password again,the 2nd time the same and the 3rd to close.

Thanks in advance. :)
Title: Re: Password In A Bat File
Post by: Sidewinder on April 07, 2020, 02:32:54 PM
The missing operand is an ampersand.

Code: [Select]
) else (set /a tries=%tries% -1 & goto :checker)

There is also no exit after 3 attempts. When I tested your code, I was informed I had -6 attempts.
FYI: using the /i switch on the if is case insensitive. This would make PaSsWoRd and password equal; your choice

This may help:
Code: [Select]
@echo off
setlocal enabledelayedexpansion
set pswd=PaSsWoRd

for /l %%i in (3, -1, 1) do (
  echo You Have %%i Attempts Left
  set /p pass=Enter Password:
  if /i !pswd!==!pass! (echo Secret Message | clip) & goto continue
  if %%i==1 echo Failed Security Check & exit /b
)
:continue

Good luck.  8)