You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Batch脚本输入正确时仍偶尔退出的问题求助

问题分析与修复方案

你的脚本输入"y"后意外退出,核心原因是变量延迟扩展未启用、变量未初始化、语法错误这几点,以下是具体修复说明和修正后的完整脚本:

关键问题与修复点

  • 启用变量延迟扩展:Batch解析if/for代码块时会提前替换所有%变量%,如果变量是在块内设置的,会使用旧值或空值导致判断失效。添加setlocal enabledelayedexpansion启用延迟扩展,块内用!变量!替代%变量%引用。
  • 初始化set/p变量:每次使用set /p前清空变量,避免用户直接回车时沿用之前的变量值,引发错误判断。
  • 修正语法错误:原脚本中cd replugged pnpm i && pnpm build缺少&&,改为cd /d "%userprofile%\replugged" && pnpm i && pnpm build,否则cd会把pnpm当作目录名,执行失败触发退出。
  • 优化errorlevel判断:用if not errorlevel 1替代if %errorlevel% == 0,这种方式更可靠,避免errorlevel为非数字值时的语法错误。
  • 修正引号使用:把原脚本中的中文引号替换为英文引号,确保提示文本正常显示。

完整修正后的脚本

@echo off
setlocal enabledelayedexpansion

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

:check_nodejs
echo Checking if Node.js is installed...
where npm >nul 2>&1
if not errorlevel 1 (
  echo Node.js is already installed.
  goto check_replugged
) else (
  echo Node.js is not installed on this system.
  set "install="
  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 not errorlevel 1 (
  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...
if exist "%userprofile%\replugged" (
  echo replugged is already installed.
  goto check_discord
) else (
  set "install="
  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
  )
)

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

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

if /i "!discordversion!" == "stable" (
  echo Stopping Discord process...
  taskkill /f /im discord.exe >nul 2>&1
  if errorlevel 1 (
    echo Failed to stop Discord process.
  )
) else if /i "!discordversion!" == "ptb" (
  echo Stopping DiscordPTB process...
  taskkill /f /im discordptb.exe >nul 2>&1
  if errorlevel 1 (
    echo Failed to stop DiscordPTB process.
  )
) else if /i "!discordversion!" == "canary" (
  echo Stopping DiscordCanary process...
  taskkill /f /im discordcanary.exe >nul 2>&1
  if errorlevel 1 (
    echo Failed to stop DiscordCanary process.
  )
) else if /i "!discordversion!" == "development" (
  echo Stopping DiscordDevelopment process...
  taskkill /f /im discorddevelopment.exe >nul 2>&1
  if errorlevel 1 (
    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 errorlevel 1 (
  echo Failed to install pnpm.
)

echo Changing to %userprofile%\replugged directory...
PUSHD "%userprofile%\replugged" >nul 2>&1
if errorlevel 1 (
  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 errorlevel 1 (
  echo Failed to update global git configuration.
)

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

echo Unplugging !discordversion!...
call pnpm run unplug !discordversion! >nul 2>&1
if errorlevel 1 (
  echo Failed to unplug !discordversion!.
)

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

echo Plugging !discordversion!...
call pnpm run plug !discordversion! >nul 2>&1
if errorlevel 1 (
  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.
endlocal

额外优化说明

  • 把cd改为cd /d,确保跨盘符切换目录有效。
  • 调整check_connection逻辑顺序,代码更简洁。
  • 脚本末尾添加endlocal,清理局部环境变量。

内容的提问来源于stack exchange,提问作者Motzumoto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 18:52:37