0

How do I make the code ask the user to input cd "folder one" then accept their input once they correctly input the test and go to label :2.

@echo off

:BEGIN
echo cd "folder one" = navigates user to folder 1.
set /p input=Insert Word:
if /i "%input%"=="cd "folder on""  goto 2
if /i not "%input%"=="cd "folder one"" & goto BEGIN
echo.

:2
echo you did it!
timeout /t 05
exit
michael_heath
  • 5,262
  • 2
  • 12
  • 22
  • What is the error you are getting? Also, it should be `goto BEGIN`, not `& goto BEGIN`. So, your question is to check the user input (simply a variable), with a value? This is so simple... – double-beep Jan 03 '19 at 11:24
  • You have nested `"`s. Try `[%input%]==[cd "folder on"]`. See [If - Conditionally perform command - Windows CMD - SS64.com](https://ss64.com/nt/if.html) why `"` can cause problems here. – DavidPostill Jan 03 '19 at 12:57
  • Typo: `folder on` is used instead `folder one`. – double-beep Jan 03 '19 at 12:57
  • @DavidPostill the correction doesn't work, assuming I did it correctly. ---- :BEGIN echo cd "folder one" = navigates user to folder 1. set /p input=Insert Word: if /i [%input%]==[cd "folder on"] goto 2 if /i not [%input%]==[cd "folder one"] goto BEGIN echo. :2 echo you did it! timeout /t 05 exit – Iroamalone Atnight Jan 04 '19 at 01:05
  • @DavidPostill spaces are a command separator. The only way to protect them is with double quotes. The IF command does not see the angle brackets as the beginning and end of the string to compare. – Squashman Jan 04 '19 at 01:16
  • Consider accepting an answer. – double-beep Feb 11 '19 at 18:47

2 Answers2

0

Just found that; you need to escape inner double quotes. You can try:

@echo off
setlocal EnableDelayedExpansion

:BEGIN
echo cd "folder one" = navigates user to folder 1.
set /p input=Insert Word: 
set input1=cd "folder one"
if /I !input! == !input1! (goto 2) else (goto BEGIN)
echo.

:2
echo you did it^^!
timeout /t 5
exit

Using here DelayedExpansion due to percent sign symbol which causes double quotes not to be read correctly.

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • It now accepts quotes, However if I even type "a" and press enter, it echoes "you did it!" back to me. therefore, the user does not have to type the correct string to get a successful echo message in your .bat file, how can this be fixed? – Iroamalone Atnight Jan 04 '19 at 00:51
0
@echo off
setlocal enabledelayedexpansion

:BEGIN
echo cd "folder one" = navigates user to folder 1.
set "item1=cd "folder one""
set /p "input=Insert Word: "
if /i "!input!" == "!item1!" (goto 2) else goto BEGIN

:2
echo you did it^^!
timeout /t 05
exit /b

Delayed expansion of variables can help with these cases, as pre-expanded variables are expanded literally into the source prior to execution. Delayed expansion stores the value in memory and compares the value direct at execution.

To ensure correct comparison, both sides may need to be delayed expansion to avoid special character such as double quotes. Both sides may not need the outer double quotes as it does not suffer the literal syntax error issues of the pre-expanded variables. I chose to add the outer double quotes.

You may need to disable delayed expansion if you use pre-expanded variables at expand time, that may contain exclaimation marks, and enable after that time to expand the delayed expanded variables.

View setlocal /? about the use of enabledelayedexpansion and disabledelayedexpansion.

michael_heath
  • 5,262
  • 2
  • 12
  • 22