0

Passing a link as an argument doesn't work

Trying to avoid repetition by using the link as an argument to the function open12. But I can't make the argument work. It works fine if I don't use an argument for the function but insert the link directly.

Already tried using chrome and edge it didn't work. Also I tried enclosing the link with "" it didn't work either.

It seems to be a really dumb error but I can't find the solution online. I know little about batch scripting though. Thanks for reading.

@echo off

echo 1.lofi Girl
echo 2.beats to Study

set /p option = Que queres escuchar?
if "%option%" == "1" call :open12 https://www.youtube.com/watch?v=5qap5aO4i9A

:open12
start vivaldi %1
goto :eof
  • Please don't use `set /P` for a choice menu, use instead the command [choice](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/choice). For the reasons and how to use `choice` see my answer on [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) – Mofi Apr 10 '22 at 07:16
  • Thanks for the tip. I am using CHOCIE now and it works fine thank you. – Laureano Oliva Apr 11 '22 at 12:12
  • Now when I enter a wrong input nothing is registered and you hear a beeping sound. – Laureano Oliva Apr 11 '22 at 12:21
  • Yes, that is the reason why `choice.exe` is better than `set /P`. This [Windows command](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) accepts only the keys as defined by author of the batch file and not any string as `set /P`. If the user presses a wrong key, `choice.exe` outputs the beep noise to let the user know about wrong pressed key. There is unfortunately no option to suppress the beep output. – Mofi Apr 11 '22 at 13:32

1 Answers1

0

1. It seems that the spaces in ...option = prompt are invalid.
2. The = in the link is converted to space (you can try to output %1 and %2, %2 is actually not empty).
Not even escaping by ^= works.
But with quotes it works perfectly fine in my test (with edge in my case)

@echo off

echo 1.lofi Girl
echo 2.beats to Study

set /p "option=Que queres escuchar? "
if "%option%" == "1" (
    call :open12 "https://www.youtube.com/watch?v=abcdefg"
)

:open12
start msedge %1
goto :eof
Magoo
  • 77,302
  • 8
  • 62
  • 84
KekuSemau
  • 6,830
  • 4
  • 24
  • 34
  • Also, note that the `=` must ***immediately*** follow the variable name, otherwise the space will be **included** in the variable name set. – Magoo Apr 10 '22 at 08:30
  • I don't see how leaving strikethrough text improves the answer, but well, if it helps you... – KekuSemau Apr 10 '22 at 08:56
  • Because the change made to your `set /p` code (insertion of the quotes) invalidates point (1) – Magoo Apr 10 '22 at 08:59
  • This code executes always the command line `start msedge %1` independent on using entering `1` or `2` or no string at all or `hello girl`. It is not complete and insecure because of this code can be used by a hacker to run code not written in the batch file at all. My answers on [Where does GOTO :EOF return to?](https://stackoverflow.com/a/37518000/3074564) and [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) could be useful to improve the code to make the batch file fail safe and secure. – Mofi Apr 11 '22 at 13:39