0

I made a batch script that asks the user if they want to do something and they either provide y/n. If the user provides "y" it'll move forward, if they provide "n" the script will exit. Sometimes when providing "y" the script exits anyway. I don't know how to fix this because I haven't seen anyone else with this issue. Here is my script:

@echo off

:check_connection
cls
echo Checking for internet connection...
ping -n 1 google.com >nul 2>&1
if not %errorlevel% == 0 (
  echo No internet connection detected. Waiting 15 seconds before trying again.
  timeout /t 15 >nul 2>&1
  goto check_connection
) else (
  echo Internet connection detected. Proceeding with script.
)

:check_nodejs
echo Checking if Node.js is installed...
where npm >nul 2>&1
if %errorlevel% == 0 (
  echo Node.js is already installed.
  goto check_replugged
) else (
  echo Node.js is not installed on this system.
  set /p install="Do you want to install it now? (y/n) "
  if /i "%install%" == "y" (
    goto install_nodejs
  ) else (
    echo Exiting script.
    goto :eof
  )
)

:install_nodejs
echo Downloading Node.js installer...
bitsadmin /transfer "nodejs_installer" /download /priority high "https://nodejs.org/dist/v18.13.0/node-v18.13.0-x64.msi" "%temp%\node-v18.13.0-x64.msi"
echo Installing Node.js...
start "" /wait msiexec /i "%temp%\node-v18.13.0-x64.msi" /qn
if %ERRORLEVEL% == 0 (
  echo Node.js installation successful.
  goto check_nodejs
) else (
  echo Node.js installation failed. Exiting script.
  start "" "https://nodejs.org/en/download/"
  goto :eof
)

:check_replugged
echo Checking if replugged is installed...
cd %userprofile%\replugged
if not exist "%userprofile%\replugged" (
  set /p install="replugged is not installed on this system. Do you want to install it now? (y/n) "
  if /i "%install%" == "y" (
    goto install_replugged
  ) else (
    echo Exiting script.
    goto :eof
  )
) else (
  echo replugged is already installed.
  goto check_discord
)

:install_replugged
echo Installing replugged...
cd %userprofile%
if not exist "%userprofile%\replugged" (
  git clone https://github.com/replugged-org/replugged
)
cd replugged pnpm i && pnpm build
if not %errorlevel% == 0 (
  echo Failed to install replugged. Exiting script.
  goto :eof
) else (
  echo replugged installation successful.
  goto check_discord
)


:check_discord
set /p discordversion="Which discord version you want to install replugged on? (stable,ptb,canary,development) [stable]: " /t 20
if "%discordversion%" == "" (set discordversion=stable)


if /i "%discordversion%" == "stable" (
  echo Stopping Discord process...
  taskkill /f /im discord.exe >nul 2>&1
  if not %errorlevel% == 0 (
    echo Failed to stop Discord process.
  )
) else if /i "%discordversion%" == "ptb" (
  echo Stopping DiscordPTB process...
  taskkill /f /im discordptb.exe >nul 2>&1
  if not %errorlevel% == 0 (
    echo Failed to stop DiscordPTB process.
  )
) else if /i "%discordversion%" == "canary" (
  echo Stopping DiscordCanary process...
  taskkill /f /im discordcanary.exe >nul 2>&1
  if not %errorlevel% == 0 (
    echo Failed to stop DiscordCanary process.
  )
) else if /i "%discordversion%" == "development" (
  echo Stopping DiscordDevelopment process...
  taskkill /f /im discorddevelopment.exe >nul 2>&1
  if not %errorlevel% == 0 (
    echo Failed to stop DiscordDevelopment process.
  )
) else (
  echo Invalid input. Exiting script.
  goto :eof
)

echo Installing pnpm...
call npm i -g pnpm >nul 2>&1
if not %errorlevel% == 0 (
  echo Failed to install pnpm.
)

echo Changing to %userprofile%\replugged directory...
PUSHD %userprofile%\replugged >nul 2>&1
if not %errorlevel% == 0 (
  echo Failed to change to %userprofile%\replugged directory.
)

echo Updating global git configuration...
call git config --global --add safe.directory %userprofile%\replugged >nul 2>&1
if not %errorlevel% == 0 (
  echo Failed to update global git configuration.
)

echo Pulling latest changes from Git repository...
call git pull >nul 2>&1
if not %errorlevel% == 0 (
  echo Failed to pull latest changes from Git repository.
)

echo Unplugging %discordversion%...
call pnpm run unplug %discordversion% >nul 2>&1
if not %errorlevel% == 0 (
  echo Failed to unplug %discordversion%.
)

echo Installing dependencies...
call pnpm i >nul 2>&1
if not %errorlevel% == 0 (
  echo Failed to install dependencies.
)
echo Building project...
call pnpm run build >nul 2>&1
if not %errorlevel% == 0 (
  echo Failed to build project.
)

echo Plugging %discordversion%...
call pnpm run plug %discordversion% >nul 2>&1
if not %errorlevel% == 0 (
  echo Failed to plug %discordversion%.
)

echo Launching %discordversion% update process...
if /i "%discordversion%" == "stable" (
  START "" "%localappdata%\Discord\Update.exe" --processStart discord.exe
) else if /i "%discordversion%" == "ptb" (
  START "" "%localappdata%\DiscordPTB\Update.exe" --processStart discordptb.exe
) else if /i "%discordversion%" == "canary" (
  START "" "%localappdata%\DiscordCanary\Update.exe" --processStart discordcanary.exe
) else if /i "%discordversion%" == "development" (
  START "" "%localappdata%\DiscordDevelopment\Update.exe" --processStart discorddevelopment.exe
)

echo Restoring original current directory...
POPD

echo Done.

It happens with all inputs hence why I've provided the entire thing

I've tried changing the input to accept both "y " and "y" but neither work.

Motzumoto
  • 9
  • 3
  • 2
    [reason, why it doesn't work](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028). Consider using [choice](https://ss64.com/nt/choice.html) instead of `set /p`. It's much more robust against wrong input (it just doesn't accept it) – Stephan Jan 28 '23 at 09:40
  • Pinging Google only determines whether you have an internet connection if you get a successful response. If you don't get one, it doesn't mean you don't have an internet connection It could mean the Google didn't respond, didn't respond quickly enough, or that DNS failed to translate google.com.into an IP address. You don't need google.com to be up for your task, but you may need, nodejs.org, github.com, or probably discord.com. it would therefore make more sense, if you're going to use ping at all, to check for a connection to one or all of those, at a more appropriate time in your script. – Compo Jan 28 '23 at 13:59
  • I chose to ping google because it's reliable, only gone down 3–4 times in the last few years, but yeah I do see what you're saying. I need a better option for checking for an internet connection, it was the simplest thing I could think of at the time. – Motzumoto Jan 29 '23 at 17:04

1 Answers1

0

Few issues:

if %errorlevel% == 0 (
  echo Node.js is already installed.
  goto check_replugged
) else (
 echo Node.js is not installed on this system.
  set /p install="Do you want to install it now? (y/n) "
  if /i "%install%" == "y" (
    goto install_nodejs
  ) else (
    echo Exiting script.
    goto :eof
  )
)

If errorlevel is 0, the code will goto a label, so the else clause is not required.

Try using

if %errorlevel% == 0 (
  echo Node.js is already installed.
  goto check_replugged
) 
echo Node.js is not installed on this system.
set /p install="Do you want to install it now? (y/n) "
if /i "%install%" == "y" (
  goto install_nodejs
 ) 
echo Exiting script.
goto :eof

Next, it's usual practice to add a

setlocal

command directly after the @echo off. The effect is that any changes made to the environment variables made by the script are undone when the script ends. Without that mechanism, you would need to manually reset the variables changed, or initialise the variables you want to use. If you don't reset/initialise the variables, then they retain their values from the previous run, until they are specifically altered.

Next, SET /P "var=Prompt" does not change var if Enter alone is pressed. Consequently, if var is originally empty, it remains empty. Hence, it's advisable that you set the variable used first (sometimes useful to set it to a value to be used by default so the user just needs to press Enter to use the default value)

Finally, your code uses set /p to alter a variable's value within a code block (parenthesised sequence of commands).

Whereas removing the else clauses will make this observation largely redundant for the current code, you need to know about the delayed expansion trap

Within a block statement, the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered, not as it may be changed within the (block).

Magoo
  • 77,302
  • 8
  • 62
  • 84