-1

I am trying to make a batch file that prompts the user and asks which website they want to open, but i can't get it to work. Here is my code:

@echo off
echo ====================
echo Website:
echo ====================

set /p "site = youtube(y), google(g): "
if /i "%site%" == "y"(
start chrome https://youtube.com
)
Pause

The if statement does not work for me, even though i input a y it doesn't launch youtube, but skips ahead to pause. Can someone tell me how to use the if statement properly?

lbert
  • 21
  • 3
  • 1
    What does *does not work for me* mean specifically? – Ken White May 12 '20 at 19:11
  • 4
    take a look at `if /?`, then *add* a space before `(`. Then take a look at `set /?` and *remove* a space in the line before (better make it two). – Stephan May 12 '20 at 19:13
  • _does not work for me_ means that even though i input a _y_ it doesn't launch youtube, but skips ahead to _pause_ @KenWhite – lbert May 12 '20 at 19:26
  • Then that information needs to be in your question, because it's an actual issue. *does not work* is not. – Ken White May 12 '20 at 19:36
  • @Stephan Don't know that "*caused by typos*" is the right reason to close here. This looks more like a misunderstanding of the `set/p` syntax. If anything, it could be closed as a duplicate of [Defining and using a variable in batch file](https://stackoverflow.com/questions/10552812/defining-and-using-a-variable-in-batch-file) which answers the same question for the plain `set` statement. – dxiv May 12 '20 at 20:06
  • @dxiv - it's also caused by there not being a space between `"y"` and `(` – SomethingDark May 12 '20 at 20:26
  • @dxiv: when you tell us "the right" reason to close a question where the issue is "didn't read / misunderstood / misinterpreted the documentation / help", we'll be more than happy to use that in the future. (I'm sure I'm speaking for the other close voters too) – Stephan May 12 '20 at 20:54
  • @Stephan I do see an inconsistency between closing this question as a "*typo*", while the one I linked above was happily upvoted hundreds of times. That said, I am certainly not "*telling*" anyone what to do, just explaining my vote to reopen. – dxiv May 12 '20 at 21:02
  • @dxiv: the linked question is from 2012. Focus shifted since then (and probably will shift in the future - in whatever direction). It's quite consistent when you compare similar questions since mid 2019. As you noticed, similar questions were upvoted (considered helpful) eight years ago, but tend to be downvoted today. Is it good or bad? I don't know, but that's what we (the majority) seem to agree on nowadays. No reason for you (or anyone else) to "follow the herd" though - maybe you're a trendsetter and focus will shift back. Follow your guts - there is no right or wrong. – Stephan May 13 '20 at 13:06
  • @dxiv: and remember: a closed question does not mean "that's a terrible question", but just "there are enough similar questions that can easily be found and there is no need for an additional similar answer" – Stephan May 13 '20 at 13:08

2 Answers2

1

set /p "site = youtube(y), google(g): "

This is setting variable 'site ' (with a space at the end) but...

if /i "%site%" == "y"

...this is checking (a different) variable 'site' with no spaces in the name.

dxiv
  • 16,984
  • 2
  • 27
  • 49
0

To begin with, do not use Set /P for known user input, the Choice command was designed just for that scenario:

Example:

@Echo Off
Echo ====================
Echo Website:
Echo ====================

%__AppDir__%choice.exe /C YG /N /M "YouTube [Y], or Google [G]?"
If ErrorLevel 2 (
    Start "" "%LocalAppData%\Google\Chrome\Application\chrome.exe" "https://www.google.com"
) Else Start "" "%LocalAppData%\Google\Chrome\Application\chrome.exe" "https://www.youtube.com"
Pause

You'll note, I have used an absolute path for the Chrome executable. If you're certain that the appropriate registry setting is in place for it, you can change each instance of the command to, Start chrome.exe, otherwise change the path to the actual location of your installed executable.

If you choose the full path route, then it may be simpler to use a variable to hold it:

@Echo Off
Set "chrome=%LocalAppData%\Google\Chrome\Application\chrome.exe"

Echo ====================
Echo Website:
Echo ====================

%__AppDir__%choice.exe /C YG /N /M "YouTube [Y], or Google [G]?"
If ErrorLevel 2 (
    Start "" "%chrome%" "https://www.google.com"
) Else Start "" "%chrome%" "https://www.youtube.com"
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39