0

I'm having an issue with an URL in an if-statement using batch.

@ECHO OFF

SET /P input="Insert Link "

if %input%==l (echo true) else (echo false)

cmd /k

I want to determine if the user input is a link or a single character (in this case l). I get that the operators within the URL might cause the problem. But shouldn't the if-statement just check if %input% is l and everything else would trigger the else? Using SET %input%=l leads to the true case being triggered.

Any ideas on how to make this work in a simple way? Am I missing something regarding syntax?

2
  • 1
    If you "want to determine if the user input is"... you need to use the correct set option: Set /P "input=PromptString". The correct If command would be If "%input%" == "l" (Echo true) Else Echo false or If /I "%input%" == "l" (Echo true) Else Echo false (for L or l). Commented Oct 18, 2022 at 15:18
  • Of course. I was using SET input=https://www.youtube.com/watch?v=6zswl5YrvVw for testing purposes. I'm going to edit the OP so my question and the given code are matching. Commented Oct 19, 2022 at 8:21

2 Answers 2

0

Putting %input% and l within the if-statement in quotes solved the problem.

@ECHO OFF

SET /P input="Insert Link "

if "%input%"=="l" (echo true) else echo false

cmd /k
Sign up to request clarification or add additional context in comments.

Comments

-1

The way the Command Line works, %ThisIsAVariable% will be replaced with whatever ThisIsAVariable contains and then be interpreted as such. Hence, running your example prompts the following error:

=6zswl5YrvVw==l was unexpected at this time.

The simplest way to solve this is to wrap your %input% with ""

e.g.

@ECHO OFF

SET input=https://www.youtube.com/watch?v=6zswl5YrvVw

if "%input%"==l (echo true) else (echo false)

cmd /k

That would prompt false

3 Comments

You'll want to put quotes around the l since quotes themselves are included in the comparison. You'll also want to wrap input=https://www.youtube.com/watch?v=6zswl5YrvVw in quotes (so set "input=https://www.youtube.com/watch?v=6zswl5YrvVw").
The quotes did the trick. if "%input%"=="l" (echo true) else (echo false) it is. Thank you for your help and the explanation!
Please do not mark an answer as a solution to a problem when the answer does not solve that problem @fassimcgee. The above is so very wrong, and by accepting it you're potentially sending future readers down the wrong path. In the above code, only line 1 is acceptable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.