批量呼叫退出

时间:2017-07-29 18:08:23

标签: batch-file

所以我有一个菜单,当输入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=0123456789" %%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

1 个答案:

答案 0 :(得分:0)

您应该将exit标签作为if条件中bat条件之后的第一个标签,以简化调用并具有干净的结构:

工作示例:

<强> Test1.bat

@echo off

:MAIN
cls
echo I am in TEST1
REM Prompt the user to select what they wish to do
echo -----------------------------------
echo -        -
echo -----------------------------------
echo.
echo 1. Menu
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%==1 goto MENU
if %input%==6 goto EXIT

echo Invalid choice
pause
goto MAIN

:exit
exit

:MENU
test2.bat

<强> Test2.bat

@echo off
:MAIN
cls
echo I am in TEST2
REM Prompt the user to select what they wish to do
echo -----------------------------------
echo -        -
echo -----------------------------------
echo.
echo 1. Menu
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%==1 goto MENU
if %input%==6 goto EXIT

echo Invalid choice
pause
goto MAIN

:exit
exit

:MENU
test1.bat