jeudi 18 août 2016

How to use spaces in user-input variables for "If" and "If not" statements in Batch?

I am making a Batch script that uses user-input variables with "If" and "If not" statements. Simple? Yes. However, the "If" and "If not" statements require a space in the user-input variable, and if you type in the user-input variable with a space, that causes many problems.

Keep in mind I am fairly new to coding, but here's my script.

@echo off
:B
echo Enter password:
set /p password=
if %password% == Password WithASpace goto :A
if not %password% == Password WithASpace goto :C
:C
echo Incorrect password
cls
goto :B
:A
echo Correct password, unlocking secret FBI database.
pause

I tried putting quotation marks around the "Password WithASpace" text in the If and If Not statements. That didn't work either. The command-prompt just says "Password WithASpace" was unexpected at this time and it closes immediately.

@echo off
:B
echo Enter password:
set /p password=
if %password% == Password WithASpace goto :A
if not %password% == Password WithASpace goto :C
:C
echo Incorrect password
cls
goto :B
:A
echo Correct password, unlocking secret FBI database.
pause

So how do I fix this?

Aucun commentaire:

Enregistrer un commentaire