每次我尝试使用我的一个功能(如果/否)它崩溃cmd - 批处理

时间:2016-08-26 14:10:53

标签: windows batch-file if-statement cmd passwords

所以我正在创建一个测试密码程序,我的问题是每当我尝试使用创建一个新的密码功能时,它崩溃了CMD,此时我对批处理编程是个新手,所以很抱歉,如果我上/下编码输入代码在这里

这是崩溃CMD的主要代码

:create
cls
if exist C:\Users\%Username%\Documents\Password.txt (
goto password1
) else (
set /p Password=What would you like your new password to be.
echo %Password% > C:\Users\%Username%\Documents\Password.txt
echo your new password is %Password%
pause
goto start
)
:password1
set /p Password1= A password has already been created to change it please  type           the last known password
for /f "Delims=" %%a in (C:\Users\%Username%\Documents\Password.txt) do (
set Text=%%a)
if %Password1%==%Text% goto correct
echo Incorrect!
pause
goto start
:correct
set /p password2=Correct! What would you like your new password to be.
echo %password2% > C:\Users\%Username%\Documents\Password.txt
echo Your new password is %password2%
pause
goto start

这里是所有代码

setlocal enabledelayedexpansion
@echo off
:start
color a
echo Welcome to the password program with this program you can securely store    personal data,
echo your options for this program are
echo.
echo Create - (Create a new password)
echo.
echo Check - (Check if your password is right)
echo.
echo Enter - (Enter to check your personal data)
echo.
set /p Program=What would you like to do.
goto !Program!

:create
cls
if exist C:\Users\!Username!\Documents\Password.txt ( 
goto password1
 ) else(
set /p Password=What would you like your new password to be.
echo !Password! > C:\Users\!Username!\Documents\Password.txt
echo your new password is !Password!
pause
goto start
 )
:password1
set /p Password1= A password has already been created to change it please type     the last known password
for /f "Delims=" %%a in (C:\Users\!Username!\Documents\Password.txt) do (
set Text=%%a)
if !Password1!==!Text! goto correct
echo Incorrect!
pause
goto start

:correct 
set /p password2=Correct! What would you like your new password to be.
echo !password2! > C:\Users\!Username!\Documents\Password.txt
echo Your new password is !password2!
pause
goto start

REM Not using this at the moment
:check
cls

REM Not using this at the moment
:enter
cls

对不起,如果在我刚接触批量编码之前已经回答了这个问题

1 个答案:

答案 0 :(得分:1)

您遇到的问题与批处理脚本中的变量扩展有关。

自您的代码以来,例如

(
set /p Password=What would you like your new password to be.
echo %Password% > C:\Users\%Username%\Documents\Password.txt
echo your new password is %Password%
pause
goto start
)

在块中,%Password%保留块之前的值(即第一次运行时为空)。

使用延迟扩展来解决这个问题(参见前面的链接)。