为什么CMD提示说明"set was unexpected at this time"



我正在尝试为我的一个批处理文件程序中的设置做出用户名更改选项,但是它一直显示出像"设置"此时出乎意料的消息,这很奇怪,因为我的代码似乎正确。它在输入" cuser"的价值之后发生。我要输入的输入值都没有零,因为我已经在程序开头预言了该值以使其制作它,所以我没有任何null值错误。

:uch
cls
echo.
echo Are you sure you want to change your username?
echo.
echo [Y/N]
echo.
set /p input=
if %input% EQU n goto set
if %input% NEQ y goto uch
:ucy
cls
echo.
echo Enter your current username
echo.
set /p cuser=
if %cuser% NEQ %username1% (
echo.
echo Incorrect username. Please try again.
echo Press any button to continue.
echo.
pause>null
goto :ucy
)
if %cuser% EQU %username1% (
echo Please enter new username.
echo.
set /p nuser=
echo Please enter again.
echo.
set /p nuser2=  
if %nuser2% EQU %nuser% set username1=%nuser%
if %nuser2% EQU %nuser% goto ga1
if %nuser2% NEQ %nuser% (
echo Usernames do not match. Please try again.  
echo Press any button to continue.
echo.
pause>null
goto ucy
)
goto ucy

您正在尝试设置内部内部的变量值,如果块而没有延迟扩展,并且IF用错误的语法解析,并且您有一个未封闭的括号...

:uch
cls
echo.
echo Are you sure you want to change your username?
echo.
echo [Y/N]
echo.
set /p input=
if %input% EQU n goto set
if %input% NEQ y goto uch
:ucy
cls
echo.
echo Enter your current username
echo.
set /p cuser=
if %cuser% NEQ %username1% (
    echo.
    echo Incorrect username. Please try again.
    echo Press any button to continue.
    echo.
    pause>null
    goto :ucy
)
setlocal enableDelayedExpansion
if %cuser% EQU %username1% (
    echo Please enter new username.
    echo.
    set /p nuser=
    echo Please enter again.
    echo.
    set /p nuser2=  
    if !nuser2! EQU !nuser! set username1=!nuser!
    if !nuser2! EQU !nuser! goto ga1
    if !nuser2! NEQ !nuser! (
        echo Usernames do not match. Please try again.  
        echo Press any button to continue.
        echo.
        pause>null
        goto ucy
)
goto ucy

您没有为if命令使用适当的语法。比较字符串时,使用
if "%var1%"=="%var2%"进行比较。EQL NEQ等用于数字比较。
您不需要具有多个if语句(一个是是,一个是否),因为您可以假设,如果他们不说yes,那么他们打算拒绝。这是一个改进的脚本,您的脚本不需要延迟扩展就可以工作。希望这会有所帮助。

:uch
cls
echo.
echo Are you sure you want to change your username?
set /p input=[Y/N]
if not "%input%"=="y" (goto ga1)
::THEY WANT TO CHANGE THEIR USERNAME
cls
echo.
set /p cuser=Enter your current username ^> 
if not "%cuser%"=="%username1%" (
echo.
echo Incorrect username. Please try again.
echo Press any button to continue.
pause>NUL
goto :uch
)
::THE USERNAME MATCHES
echo.
set /p nuser=Please enter new username. ^> 
echo.
set /p nuser2=Please enter again. ^>  
if "%nuser2%"=="%nuser%" (set username1=%nuser% && goto ga1)
echo Usernames do not match. Please try again.  
echo Press any button to continue.
echo.
pause>NUL
goto uch
:ga1
:://DO SOME OTHER STUFF HERE AFTER THEY CHANGED THEIR NAME OR OPTED NOT TO.

相关内容

最新更新