为什么CMD提示声明"设置此时是意外的"

时间:2016-12-09 15:49:25

标签: windows batch-file

我试图为我的一个批处理文件程序中的设置创建一个用户名更改选项,但它会一直显示一条消息,例如"此时设置是意外的"这很奇怪,因为我的代码看似正确。它输入" cuser"的值后发生。我输入输入的值都不为空,因为我已经预先声明了程序开头的值,因此我没有任何空值错误。 / p>

: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

2 个答案:

答案 0 :(得分:0)

你试图在没有delayed expansion的情况下设置变量值,并且if语法分析错误。你有一个未闭合的括号......

{{1}}

答案 1 :(得分:0)

您没有使用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.