所以我有一個菜單,當輸入5時會調用打開另一個批處理文件。然後在那個文件中我有另一個菜單,當輸入5時會調用打開第一個批處理文件。這個過程完美地工作,但是當我嘗試退出時出現問題。如果在兩個文件之間來回切換後,我決定按6退出它,如果用戶除了菜單上的選項之外還輸入了其他內容,則會顯示我的錯誤消息。但它只會做同樣的次數,我來回切換。批量退出
例如,假設我在第一個菜單中加載,然後選擇5轉到其他菜單。然後,如果我再次選擇5返回到第一個菜單並輸入6退出,它將顯示我的錯誤並將我發回菜單,如果我再按6將退出。
這是第一個菜單代碼:
setlocal enabledelayedexpansion
cls
@echo off
REM Ensure that the accounts text file exists
if exist accounts.txt (
goto LOG
) else (
echo. >accounts.txt
)
REM ensure that the transaction log file exists
:LOG
if exist log.txt (
goto MAIN
) else (
echo. >>log.txt
)
:MAIN
cls
REM Prompt the user to select what they wish to do
echo -----------------------------------
echo - -
echo -----------------------------------
echo.
echo 1. Display Account Balance
echo 2. Deposit to an Account
echo 3. Withdraw From an Account
echo 4. Show Account Activities
echo 5. Account Maintenance
echo 6. Exit
echo.
set input=
set /p input=Type your selection then press ENTER:
REM Send the user to the correct screen based on their input
if !input! gtr 6 goto ERROR
if !input! lss 1 goto ERROR
if !input!==1 goto DISPLAY
if !input!==2 goto DEPOSIT
if !input!==3 goto WITHDRAW
if !input!==4 goto SHOW
if !input!==5 goto PASS
if !input!==6 goto EXIT
REM If no option exists display error then re-prompt
:ERROR
cls
echo ERROR^^! INPUT IS INVALID^^!
echo.
echo Press any key to try again...
pause>nul
goto MAIN
:DISPLAY
cls
echo -----------------------------------
echo - Display Account Balance -
echo -----------------------------------
echo.
set num=
set space=
set flag=0
REM Prompt the user to enter their account number
set /p num=Please enter your account number:
REM Ensure that the user enters something
if defined num (
REM Locate the users account number in the accounts text file
for /f "tokens=1-4 delims=," %%a in (accounts.txt) do (
REM If account number exists, display to them their balance
if %%a==!num! (
echo.
echo Welcome back %%b^^!
echo Your current account balance is $%%c
echo.
set flag=1
)
)
REM If nothing is entered, display error then re-prompt
) else (
echo ERROR^^! You must enter an account number^^!
echo.
echo Press any key to try again...
pause>nul
goto DISPLAY
)
REM If account number does not exist, display error then re-prompt
if !flag!==0 (
echo ERROR^^! Account Number not Found^^!
echo.
echo Press any key to try again...
pause>nul
goto DISPLAY
)
REM After output has been display to the user prompt them to return to menu
echo Press any key to return to menu...
pause>nul
goto MAIN
:DEPOSIT
cls
echo -----------------------------------
echo - Deposit to an Account -
echo -----------------------------------
echo.
set num=
set deposit=
REM Prompt the user to enter their account number
set /p num=Please enter your account number:
REM If something is entered save it to a tmp file
if DEFINED num (
echo !num!> num.tmp
REM If nothing is entered display error then re-prompt
) else (
echo.
echo An account number must be entered.
echo Press any key to try again...
pause>nul
goto DEPOSIT
)
REM Locate what the user entered for validation perposes
for %%? in (num.tmp) do (
REM Check the length of the string
set /a sum=%%~z? - 2
REM If the user enters a value of 5 digits proceed
if !sum! EQU 5 (
REM Locate the string with account number the user entered (nul used to hide the findstr)
>nul findstr /b !num! accounts.txt && (
REM Prompt the user to enter the ammount they wish to deposit
set /p deposit=Enter the deposit amount:
REM Set a variable equal to the deposit amount in order to run validation
set /a eval=deposit
REM If statement needed for its else and inside validation
if !eval! EQU !deposit! (
REM If the user entered a negative number, display error then re-prompt
if !deposit! LSS 0 (
echo.
echo ERROR^^! Deposit Amount Must be Positive^^!
echo.
echo Press any key to try again...
pause>nul
goto DEPOSIT
)
REM If the user entered 0, display error then re-prompt
if !deposit! EQU 0 (
echo.
echo ERROR^^! Deposit Amount Must be greater than 0^^!
echo.
echo Press any key to try again...
pause>nul
goto DEPOSIT
)
REM If the user entered a decimal number or anything besides a number, display error then re-prompt
) else (
echo.
echo ERROR^^! Only a Positive Whole Number may be entered^^!
echo.
echo Press any key to try again...
pause>nul
goto DEPOSIT
)
REM If all validation passed locate the supplied account number and add the deposit ammount to their account
for /f "tokens=1-5 delims=," %%r in (accounts.txt) do (
if %%r==!num! (
set /a new_amount=%%t+!deposit!
echo %%r,%%s,!new_amount! >> accounts.tmp
echo.
echo $!deposit! have been added to your account. Your new balance is $!new_amount!.
echo.
REM Create a transaction to be added to the log file
echo %date%,!num!,Deposit,!deposit!>>log.txt
REM Leave all other accounts untouched
) else (
echo %%r,%%s,%%t >> accounts.tmp
)
)
REM If the account number does not exist, display error then re-prompt
) || (
echo.
echo ERROR^^! Account Number Not Found^^!
echo.
echo Press any key to try again...
pause>nul
goto DEPOSIT
)
REM Delete the tmp file needed for validation
del /f num.tmp
REM If the account number entered is less than 5 digits, display error then re-prompt
) else (
echo.
echo Account number must be 5 digits long.
echo.
pause
goto DEPOSIT
del /f num.tmp
)
)
REM Copy temp file to txt file
copy accounts.tmp accounts.txt >nul
del /f accounts.tmp
echo Press any key to return to menu...
pause>nul
goto MAIN
:WITHDRAW
cls
echo -----------------------------------
echo - Withdraw to an Account -
echo -----------------------------------
echo.
set num=
set withdraw=
REM Prompt the user to enter their account number
set /p num=Please enter your account number:
REM If something is entered save it to a tmp file
if DEFINED num (
echo !num!> num.tmp
REM If nothing is entered display error then re-prompt
) else (
echo.
echo An account number must be entered.
echo Press any key to try again...
pause>nul
goto WITHDRAW
)
REM Locate what the user entered for validation perposes
for %%? in (num.tmp) do (
REM Check the length of the string
set /a sum=%%~z? - 2
REM If the user enters a value of 5 digits proceed
if !sum! EQU 5 (
REM Locate the string with account number the user entered (nul used to hide the findstr)
>nul findstr /b !num! accounts.txt && (
REM Prompt the user to enter the ammount they wish to withdraw
set /p withdraw=Enter the withdrawl amount:
REM Set a variable equal to the withdraw amount in order to run validation
set /a eval=withdraw
REM If statement needed for its else and inside validation
if !eval! EQU !withdraw! (
REM If the user entered a negative number, display error then re-prompt
if !withdraw! LSS 0 (
echo.
echo ERROR^^! Withdrawl Amount Must be Positive^^!
echo.
echo Press any key to try again...
pause>nul
goto WITHDRAW
)
REM If the user entered 0, display error then re-prompt
if !withdraw! EQU 0 (
echo.
echo ERROR^^! Withdrawl Amount Must be greater than 0^^!
echo.
echo Press any key to try again...
pause>nul
goto WITHDRAW
)
REM If the user entered a decimal number or anything besides a number, display error then re-prompt
) else (
echo.
echo ERROR^^! Only a Positive Whole Number may be entered^^!
echo.
echo Press any key to try again...
pause>nul
goto WITHDRAW
)
REM If all validation passed locate the supplied account number and remove the withdraw ammount from their account
for /f "tokens=1-5 delims=," %%r in (accounts.txt) do (
if %%r==!num! (
set /a new_amount=%%t-!withdraw!
echo.
echo %%r,%%s,!new_amount! >> accounts.tmp
echo $!withdraw! have been withdrawn from your account. Your new balance is $!new_amount!.
echo.
REM Create a transaction to be added to the log file
echo %date%,!num!,Withdraw,!withdraw!>>log.txt
REM Leave all other accounts untouched
) else (
echo %%r,%%s,%%t >> accounts.tmp
)
)
REM If the account number does not exist, display error then re-prompt
) || (
echo.
echo ERROR^^! Account Number Not Found^^!
echo.
echo Press any key to try again...
pause>nul
goto WITHDRAW
)
REM Delete the tmp file needed for validation
del /f num.tmp
REM If the account number entered is less than 5 digits, display error then re-prompt
) else (
echo.
echo Account number must be 5 digits long.
echo.
pause
goto WITHDRAW
del /f num.tmp
)
)
REM Copy temp file to txt file
copy accounts.tmp accounts.txt >nul
del /f accounts.tmp
echo Press any key to return to menu...
pause>nul
goto MAIN
:SHOW
cls
echo -----------------------------------
echo - Show Account Activities -
echo -----------------------------------
echo.
set num=
REM Prompt the user to enter their account number
set /p num=Please enter your account number:
REM If something is entered save it to a tmp file
if DEFINED num (
echo !num!> num.tmp
REM If nothing is entered display error then re-prompt
) else (
echo.
echo Please enter an account number
echo Press any key to try again...
pause>nul
goto SHOW
)
REM Locate what the user entered for validation perposes
for %%? in (num.tmp) do (
REM Check the length of the string
set /a sum=%%~z? - 2
REM If the user enters a value of 5 digits proceed
if !sum! EQU 5 (
REM Locate the string with account number the user entered (nul used to hide the findstr)
>nul findstr /b !num! accounts.txt && (
echo.
echo Date Account Code Transaction Type Amount
echo ---------------------------------------------------------
echo.
REM Locate and display activities of entered account number
for /f "tokens=1-4 delims=," %%a in (log.txt) do (
if %%b==!num! (
echo %%a %%b %%c $%%d
)
)
REM Prompt the user to return to the main menu
echo.
echo Press any key to return to menu...
pause>nul
goto MAIN
REM If the account number does not exist, display error then re-prompt
) || (
echo.
echo ERROR^^! Account Number Not Found^^!
echo.
echo Press any key to try again...
pause>nul
goto SHOW
)
REM Delete the tmp file needed for validation
del /f num.tmp
REM If the account number entered is less than 5 digits, display error then re-prompt
) else (
echo.
echo Account number must be 5 digits long.
echo.
pause
goto SHOW
del /f num.tmp
)
)
:PASS
REM If password text file exist prompt the user to enter in the password, if not send to create one
if exist password.txt (
cls
set pass=
set /p pass=Enter in the password:
) else (
echo.
echo Password text file is missing^^!
echo Contact admin to create password.
echo.
echo Press any key to return to menu...
pause>nul
goto MAIN
)
REM Determine if the password provided by the user is correct
for /f %%v in (password.txt) do (
if %%v == !pass! (
goto menu
)
)
REM If it does not exist let the user know and reprompt
echo.
echo Password Incorect!
echo Press any key to try again...
pause>nul
goto PASS
:MENU
CALL menu.bat
:ERROR2
cls
echo ERROR^^! INPUT IS INVALID^^!
echo.
echo Press any key to try again...
pause>nul
goto MAIN
:EXIT
這裏是第二個菜單代碼:
REM Used to keep variable for duration of script
setlocal enabledelayedexpansion
cls
@echo off
REM Ensure that the accounts text file exists
if exist accounts.txt (
goto START
) else (
echo. >accounts.txt
)
:START
cls
REM Prompt the user to select what they wish to do
echo -----------------------------------
echo - Account Maintenance -
echo -----------------------------------
echo.
echo 1. Create an Account
echo 2. Delete an Account
echo 3. List all Accounts
echo 4. Roll-back Transactions
echo 5. Return to Previous Page
echo.
set input=
set /p input=Type your selection then press ENTER:
REM Send the user to the correct screen based on their input
if !input! gtr 5 goto ERROR
if !input! lss 1 goto ERROR
if !input!==1 goto CREATE
if !input!==2 goto DELETE
if !input!==3 goto LIST
if !input!==4 goto ROLL
if !input!==5 goto BACK
REM If no option exists display error then re-prompt
:ERROR
cls
echo ERROR^^! INPUT IS INVALID^^!
echo.
echo Press any key to try again...
pause>nul
goto START
:CREATE
cls
echo -----------------------------------
echo - Create an Account -
echo -----------------------------------
echo.
REM Clear all variables
set num=
set name=
set balance=
set var=
REM Prompt the user to enter an account number
set /p num=Enter an account number of 5 digits:
REM Check if value entered is not a number, display error then re-prompt
for /f "delims=" %%a in ("!num!") do set var=%%a
if defined var (
echo.
echo The account number must be a numeric value.
echo Press any key to try again...
pause>nul
goto CREATE
)
REM If something is entered save it to a tmp file
if defined num (
echo !num!> num.tmp
REM If nothing is entered display error then re-prompt
) else (
echo.
echo Please enter an account number.
echo.
echo Press any key to try again...
pause>nul
goto CREATE
)
REM Locate what the user entered for validation perposes
for %%? in (num.tmp) do (
REM Check the length of the string
set /a _sum=%%~z?-2
REM If the user enters a value of 5 digits proceed
if !_sum! EQU 5 (
REM If account already exists, display error then re-prompt
>nul findstr /b !num! accounts.txt && (
echo.
echo The account already exists
echo.
echo Press any key to try again...
pause>nul
goto CREATE
REM If account number doesn't exist proceed
) || (
REM Prompt the user to enter a name
set /p name=Enter a full name:
REM If something is entered proceed
if defined name (
REM Prompt the user to enter the initial deposit
set /p balance=Enter the amount of the initial deposit:
REM Set a variable equal to the balance amount in order to run validation
set /a eval=balance
REM If statement needed for its else and inside validation
if !eval! EQU !balance! (
REM If the user entered a negative number, display error then re-prompt
if !balance! LSS 0 (
echo.
echo ERROR^^! Deposit Amount Must be Positive^^!
echo.
echo Press any key to try again...
pause>nul
goto CREATE
)
REM If the user entered 0, display error then re-prompt
if !balance! EQU 0 (
echo.
echo ERROR^^! Deposit Amount Must be greater than 0^^!
echo.
echo Press any key to try again...
pause>nul
goto CREATE
)
REM If the user entered a decimal number or anything besides a number, display error then re-prompt
) else (
echo.
echo ERROR^^! Only a Positive Whole Number may be entered^^!
echo.
echo Press any key to try again...
pause>nul
goto CREATE
)
REM If all validation is passed add account to accounts text file
if defined balance (
echo !num!,!name!,!balance! >>accounts.txt
echo.
echo Press any key to return to the menu...
pause>nul
goto START
REM If no initial amount is entered, display error then re-prompt
) else (
echo.
echo Please enter an initial amout to deposit into the account..
echo.
echo Press any key to try again...
pause>nul
goto CREATE
)
REM If not name is entered, display error then re-prompt
) else (
echo Please enter your full name.
echo.
echo Press any key to try again...
pause>nul
goto CREATE
)
)
REM Delete the tmp file needed for validation
del num.tmp
REM If the account number entered is less than 5 digits, display error then re-prompt
) else (
echo.
echo Account number must be 5 digits long.
echo.
pause
goto CREATE
del /f num.tmp
)
)
:DELETE
cls
echo -----------------------------------
echo - Delete an Account -
echo -----------------------------------
echo.
set flag=0
set /p num=Enter your account number:
if defined num (
for /f "tokens=1-5 delims=, " %%a in (accounts.txt) do (
if %%a==!num! (
findstr /v !num! accounts.txt > accounts.tmp
set flag=1
set num=
)
)
) else (
echo.
echo Please enter an account number.
echo Press any key to try again...
pause>nul
goto DELETE
)
if !flag!==0 (
echo.
echo Account NOT found.
echo Press any key to try again...
pause>nul
goto DELETE
)
copy accounts.tmp accounts.txt >nul
del accounts.tmp
echo Press any key to return to the menu...
pause>nul
goto START
:LIST
cls
echo ---------------------------------------
echo - Display Accounts Information -
echo ---------------------------------------
echo.
echo Account No. Account Name Amount
echo.
for /f "tokens=1-4 delims=, " %%a in (accounts.txt) do (
echo %%a %%b %%c %%d
)
echo.
echo Press any key to return to the menu...
pause>nul
goto START
:ROLL
:ERROR2
cls
echo ERROR^^! INPUT IS INVALID^^!
echo.
echo Press any key to try again...
pause>nul
goto START
:BACK
CALL final.bat
:EXIT
你已經過修剪你的代碼。什麼是':menu'例程的剩餘部分,'final.bat'代碼的':back'例程,輸入語句在哪裏? – Magoo
@Magoo我已經添加了其餘的代碼 – Brett
也許把'exit'放在你的':exit'中......標籤 – SachaDee