1

It's a simple script I wrote, nothing fancy at all

@echo off

echo 1 = stream1 source
echo 2 = stream1 high
echo 3 = stream1 medium
echo 4 = stream1 low
echo 5 = stream2 source
echo 6 = stream2 high
echo 7 = stream2 medium
echo 8 = stream2 low

set /p id="Enter ID: "

IF %id% == 1 (
    set link=twitch.tv/stream1 source
) ELSE IF %id% == 2 (
    set link=twitch.tv/stream1 high
) ELSE IF %id% == 3 (
    set link=twitch.tv/stream1 medium
) ELSE IF %id% == 4 (
    set link=twitch.tv/stream1 low
) ELSE IF %id% == 5 (
    set link=twitch.tv/stream2 source
) ELSE IF %id% == 6 (
    set link=twitch.tv/stream2 high
) ELSE IF %id% == 7 (
    set link=twitch.tv/stream2 medium
) ELSE IF %id% == 8 (
    set link=twitch.tv/stream1 high
) ELSE (
echo ..............
echo 1 = source
echo 2 = high
echo 3 = medium
echo 4 = low

set /p quality="Enter ID: "

    IF %quality% == 1 (
        set hq="source"
    ) ELSE IF %quality% == 2 (
        set hq="high"
    ) ELSE IF %quality% == 3 (
        set hq="medium"
    ) ELSE IF %quality% == 4 (
        set hq="low"
    ) ELSE (
        set hq="source"
    )
    set link=twitch.tv/%id% %hq%
)

Start C:\livestreamer\livestreamer.exe %link%

echo %link%

pause

What it does is ask for a number 1-8, if it's 1-8 it'll run the program livestream.exe with command line stream1 source, stream2 source, etc depending on what the user input.

If it's not 1-8, then the program asks for the quality, so source/high/medium/low.

For example, if they put in 1, then the command that'll finally be run is

Start C:\livestreamer\livestreamer.exe stream1 source

If 2,

Start C:\livestreamer\livestreamer.exe stream1 high

etc

If they put in for example asdfasdf

then put in high, then the final will be

Start C:\livestreamer\livestreamer.exe asdfasdf high

It works if i remove the stuff inside the final else condition, but otherwise it doesn't :(

The error I'm getting is

( was unexpected at this time.
Jo Jo
  • 55
  • 1
  • 2
  • 4

4 Answers4

1

First thing first, batch wasn't a powerful language.
I faced same problem as you did, so I realized that the brackets need to be in that exact format otherwise it doesn’t work at all. Even putting brackets around the if part of the statement will stop the script from working as expected.

You can use choice statement, a simple statement that determine user input.

@echo off


:point_one
echo 1 = stream1 source
echo 2 = stream1 high
echo 3 = stream1 medium
echo 4 = stream1 low
echo 5 = stream2 source
echo 6 = stream2 high
echo 7 = stream2 medium
echo 8 = stream2 low

set /a id = 0

choice /c:123456789 /m "Enter ID: " /n

IF %ERRORLEVEL% == 1 (
    set link=twitch.tv/stream1 source
    set /a id = 1
)
IF %ERRORLEVEL% == 2 (
    set link=twitch.tv/stream1 high
    set /a id = 2
) 
IF %ERRORLEVEL% == 3 (
    set link=twitch.tv/stream1 medium
    set /a id = 3
) 
IF %ERRORLEVEL% == 4 (
    set link=twitch.tv/stream1 low
    set /a id = 4
) 
IF %ERRORLEVEL% == 5 (
    set link=twitch.tv/stream2 source
    set /a id = 5
) 
IF %ERRORLEVEL% == 6 (
    set link=twitch.tv/stream2 high
    set /a id = 6
) 
IF %ERRORLEVEL% == 7 (
    set link=twitch.tv/stream2 medium
    set /a id = 7
)
IF %ERRORLEVEL% == 8 (
    set link=twitch.tv/stream1 high
    set /a id = 8
)
IF %ERRORLEVEL% == 9 (
    goto point_two
)

goto point_three


:point_two
echo ..............
echo 1 = source
echo 2 = high
echo 3 = medium
echo 4 = low

choice /c:12345678 /m "Enter ID: " /n

IF %ERRORLEVEL% == 1 (
    set hq="source"
)
IF %ERRORLEVEL% == 2 (
    set hq="high"
)
IF %ERRORLEVEL% == 3 (
    set hq="medium"
)
IF %ERRORLEVEL% == 4 (
    set hq="low"
) 
ELSE set hq="source"

set link=twitch.tv/%id% %hq%

goto point three


:point_three

Start C:\livestreamer\livestreamer.exe %link%

echo %link%

pause >nul

I still don't understand why you add an else statement at the end. I mean why not add an extra selection for user to choose whether they want to go to :point_two

Happy Face
  • 1,061
  • 7
  • 18
  • Thank you so much. That's almost perfect. however what I need to be able to do is For the first part, when it asks for ID, you should be able to type stuff in. So for example, if I put in testasdf as the input for first part, it will take that, combined with what you choose in 2nd place, and use that. – Jo Jo Aug 01 '15 at 06:26
  • so do you mean user will go through both `:point_one` and `:point_two`? – Happy Face Aug 01 '15 at 06:37
1

Basically, you fell into the delayed expansion trap, but you can easily avoid it by simplifying your code (it is unneccessary complex). Something like:

@echo off
set hq=

echo 1 = stream1 source
echo 2 = stream1 high
echo 3 = stream1 medium
echo 4 = stream1 low
echo 5 = stream2 source
echo 6 = stream2 high
echo 7 = stream2 medium
echo 8 = stream2 low

set link=none
set /p id=Enter ID:
IF "%id%"=="1" set link=twitch.tv/stream1 source
IF "%id%"=="2" set link=twitch.tv/stream1 high
IF "%id%"=="3" set link=twitch.tv/stream1 medium
IF "%id%"=="4" set link=twitch.tv/stream1 low
IF "%id%"=="5" set link=twitch.tv/stream2 source
IF "%id%"=="6" set link=twitch.tv/stream2 high
IF "%id%"=="7" set link=twitch.tv/stream2 medium
IF "%id%"=="8" set link=twitch.tv/stream1 high

if "%link%" neq "none" goto :skip
cls
echo ..............
echo 1 = source
echo 2 = high
echo 3 = medium
echo 4 = low
echo ..............
set /p quality=Enter ID:
IF "%quality%"=="1" set hq="source"
IF "%quality%"=="2" set hq="high"
IF "%quality%"=="3" set hq="medium"
IF "%quality%"=="4" set hq="low"
REM are you sure, you want the quotes in %hq% ?

:skip
set param=twitch.tv/%link% %hq%
Start C:\livestreamer\livestreamer.exe %param%
echo %param%
pause

You had a logical quirk with your parameters in the last few lines.

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
0

Try this:

@echo off

echo 1 = stream1 source
echo 2 = stream1 high
echo 3 = stream1 medium
echo 4 = stream1 low
echo 5 = stream2 source
echo 6 = stream2 high
echo 7 = stream2 medium
echo 8 = stream2 low

set /p id=Enter ID:
::point one
IF "%id%"=="1" (
    set link=twitch.tv/stream1 source
) 
IF "%id%"=="2" (
    set link=twitch.tv/stream1 high
) 
IF "%id%"=="3" (
    set link=twitch.tv/stream1 medium
) 
IF "%id%"=="4" (
    set link=twitch.tv/stream1 low
) 
IF "%id%"=="5" (
    set link=twitch.tv/stream2 source
) 
IF "%id%"=="6" (
    set link=twitch.tv/stream2 high
) 
IF "%id%"=="7" (
    set link=twitch.tv/stream2 medium
)
IF "%id%"=="8" (
    set link=twitch.tv/stream1 high
)
::point one end
cls
echo ..............
echo 1 = source
echo 2 = high
echo 3 = medium
echo 4 = low
echo ..............
set /p quality=Enter ID:
::point two begin
    IF "%quality%"=="1" (
        set hq="source"
    ) 
    IF "%quality%"=="2" (
        set hq="high"
    ) 
    IF "%quality%"=="3" (
        set hq="medium"
    ) 
    IF "%quality%"=="4" (
        set hq="low"
    )
    set link=twitch.tv/%id% %hq%
    ::point two end

Start C:\livestreamer\livestreamer.exe %link%

echo %link%

pause

Your IF-else syntax was incorrect (from the point which i labelled point one begin to point one end) the same goes for the points which i marked point two begin to point two end. The script should work properly now.

Hope that helped!

Jahwi
  • 426
  • 3
  • 14
  • Thanks, but the thing is, my code works fine if i remove the parts starting from echo .......... to set link=twitch.tv/%id% %hq% and replace with something like set link=1. Also, for this I want it to ask for quality only if the id input is not numbers between 1 and 8 – Jo Jo Aug 01 '15 at 02:13
0

The error in your code is produced by the delayed expansion trap, as Stephan indicated in his answer: the quality variable is read inside a code block, so the %quality% expansion get the same value the variable had before the block. You need to change that expansion by !quality! and include setlocal EnableDelayedExpansionat beginning in order to fix your error.

However, you should know that the array concept is a method that programming languages use to process a series of elements in the same way. For example:

@echo off
setlocal EnableDelayedExpansion

rem Store individual elements in the array; at same time, show the menu

set i=0
for %%a in ("stream1 source" "stream1 high" "stream1 medium" "stream1 low"
            "stream2 source" "stream2 high" "stream2 medium" "stream2 low") do (
   set /A i+=1
   set "option[!i!]=%%~a"
   echo !i! = %%~a
)

set /p id="Enter ID: "

if defined option[%id%] (

   set "link=twitch.tv/!option[%id%]!"

) else (

   echo ..............

   rem The same...
   set i=0
   for %%a in (source high medium low) do (
      set /A i+=1
      set "id[!i!]=%%a"
      echo !i! = %%a
   )

   set /p quality="Enter ID: "

   if defined id[!quality!] (

      rem Note that is not possible to nest expansions this way: set hq="!id[!quality!]!"
      for %%i in (!quality!) do set hq="!id[%%i]!"

   ) else (
      set hq="source"
   )
   set link=twitch.tv/!id! !hq!
)

Start C:\livestreamer\livestreamer.exe %link%

echo %link%

pause

For a further description on array management in Batch files, see this post.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108