Reputation: 832
Does anyone know why this is not working? I want it to ask for a string, such as 1, 2 or exampletext. Then it checks if the input value is 1, then it changes the variable to ABC, if it's 2, then it changes to DEF, otherwise leave it the same.
@echo off
set /p id="Enter ID: " %=%
IF "%id%" == "1"(
set id="ABC"
)
ELSE (
IF "%id%" == "2"(
set id="DEF"
)
ELSE (
PING 127.0.0.1 -n 0.1 >nul
)
)
Start "" "C:\Users\Comp\Desktop\livestreamer-1.5.2-win32\livestreamer.exe" twitch.tv/%id% mobile_High
Upvotes: 1
Views: 529
Reputation: 67216
Although Joey already indicated the cause of the error, I can't resist the temptation to show you a different method to do this:
@echo off
setlocal EnableDelayedExpansion
rem Define the table of equivalences:
set equiv[1]=ABC
set equiv[2]=DEF
set /p "id=Enter ID: " %=%
if defined equiv[%id%] (
set id=!equiv[%id%]!
) ELSE (
PING 127.0.0.1 -n 0.1 >nul
)
Start "" "C:\Users\Comp\Desktop\livestreamer-1.5.2-win32\livestreamer.exe" twitch.tv/%id% mobile_High
This method use an array. If you are interested in this method, search for "Delayed Expansion" and "Arrays in Batch".
Upvotes: 5
Reputation: 354476
You're just missing a few spaces (before the opening parenthesis in the if
). And else
needs to go on the same line as the closing parenthesis¹:
IF "%id%" == "1" (
set id=ABC
) ELSE (
IF "%id%" == "2" (
set id=DEF
) ELSE (
PING 127.0.0.1 -n 0.1 >nul
)
)
Note also that quotes after the =
in set
are included verbatim in the variable, which isn't what you want here.
You can also simplify a bit:
if "%id%=="1" (
set id=ABC
) else if "%id%=="2" (
set id=DEF
) else (
ping localhost -n 1 >nul
)
¹ Which is even spelled out in help if
on the very first page: “The ELSE clause must occur on the same line as the command after the IF.”
Upvotes: 3