Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement not working batch (goes directly to else)

Tags:

batch-file

I'm simply working on a password for my batch file;but, my if statement is not working. when I ask it to check whether my password is correct, it goes to the else statement, even if i correctly enter the password (hi). here is the part of my code that has the problem:

if "%R%"=="hi" (
goto b
) else (
echo access denied.
goto f
)

and here is the entire code:

echo off
color 0f
pause

:f
set /p R = "Please enter your passcode "

if "%R%"=="hi" (
goto b
) else (
echo access denied.
goto f
)

:a
for /L %%A IN (1,1,234) DO (
color 6e
echo          %random%%random%%random%%random%%random%%random%%random%
color 2a
echo %random%%random%%random%%random%%random%%random%%random%%random%
color 1b
echo %random%%random%%random%%random%%random%%random%%random%%random%
color 5d
echo %random%%random%%random%%random%%random%%random%%random%%random%
color 4c
echo %random%%random%%random%%random%%random%%random%%random%%random%
)
GOTO c

:b
echo Welcome Back
pause >nul
echo Your current computer does not contain previous files.
pause >nul
echo Download Backup Files now?
pause >nul
echo Downloading all Files...
goto a

:c
echo access granted. welcome to the CIA Mainframe.
pause >nul
echo Please se-se-select a c-command.
pause >nul
echo Alert! Alert! The main Fire wall has been Breached!
pause >nul
echo Files being deleted now...
pause >nul

:d
color 0c
echo %random%%random%%random%%random%%random%%random%%random%
echo %random%%random%%random%%random%%random%%random%%random%%random%
echo %random%%random%%random%%random%%random%%random%%random%%random%
echo %random%%random%%random%%random%%random%%random%%random%%random%
echo %random%%random%%random%%random%%random%%random%%random%%random%
goto d

Can anyone please help.

like image 643
coolio Avatar asked Nov 09 '22 16:11

coolio


1 Answers

This is a problem with spaces in your variable.

Try this from a command line:

>set /p R = "Please enter your passcode: "
Please enter your passcode: blah

>echo %R%
%R%

>echo %R %
blah

Change your line to:

set /p "R=Please enter your passcode: "

And that should fix.

like image 60
Wes Larson Avatar answered Jan 04 '23 03:01

Wes Larson