Home > Mobile >  URL with operators (=, -) not working within if-statement
URL with operators (=, -) not working within if-statement

Time:10-20

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?

CodePudding user response:

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

CodePudding user response:

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

  • Related