2

How do I call multiple batch files within a single batch? When I try it always goes to the same one or none at all and closes window.

@echo off
:MENU
title MENU0
Echo 1 - Select Menu 1
Echo 2 - Select Menu 2
Echo 0 - Exit
Echo.


SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1
IF %choice%==2 GOTO 2
IF %choice%==0 EXIT

:1
call %userprofile%\desktop\\Menu1.bat

:2
call %userprofile%\desktop\Menu2.bat
halfer
  • 19,824
  • 17
  • 99
  • 186
jonnyrocket88
  • 21
  • 1
  • 3

7 Answers7

4

There are several issues with provided batch code in question.

The first one is that after processing of the batch file called with command CALL finished, the processing of current batch file continues with the next command respectively line, except the called batch file contains itself the command EXIT without parameter /B as in this case the command processor terminates itself independent on calling hierarchy.

For details about CALL behavior see answers on:

The second issue is that folder path assigned to environment variable USERPROFILE could contain 1 or more spaces (default on Windows 2000/XP, possible on later Windows versions depending on user name). Therefore always enclose a string referencing USERPROFILE or USERNAME in double quotes.

The third and most difficult to handle issue is that the user of a batch file on prompt with set /P has the freedom to enter anything and not just what the writer of the batch file suggests.

For example

SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1

results in an exit of batch processing caused by a syntax error if the batch user hits just RETURN or ENTER without entering anything at all and the environment variable choice is not already defined with a useful string because in this case the next line to process by command processor is:

IF ==1 GOTO 1

It is good practice to define the environment variable with a default value before set /P as this value is kept when the batch user just hits RETURN or ENTER.

A batch user has also the freedom on using set /P to enter anything including syntax critical characters like " or < or | or > and others by mistake or intentionally (for breaking batch processing by a syntax error).

Therefore it is in general better for menus in batch files to use the command choice (Microsoft article) because then the batch user can enter only what the writer of the batch file offers. But CHOICE is available only by default for Windows Server 2003 and later Windows. And there are different versions of choice (SS64 article with additional information) with a different set of options. So it depends on which Windows version(s) the batch file is designed for if CHOICE can be used at all.

It is also not good to name an environment variable or a label like a command although possible. Therefore choice is not a good name for an environment variable.

Here is a commented batch file with a code which avoids all those issues.

@echo off
:MainMenu
setlocal EnableDelayedExpansion
title MENU0
cls
echo 1 - Select Menu 1
echo 2 - Select Menu 2
echo 0 - Exit
echo.

rem Define 0 as default value in case of user just hits RETURN or ENTER.
set "UsersChoice=0"
set /P "UsersChoice=Type the number or letter of task you want, then press ENTER: "

rem Has the user really entered just one of the offered characters?

rem There must be nothing to process if the user has entered just 0
rem or 1 or 2. Otherwise the user's choice was either by mistake or
rem intentionally entered wrong. The string entered by the user is
rem referenced with delayed expansion to avoid an exit of batch
rem processing in case of user entered a syntax critical character.

for /F "tokens=1 delims=012" %%I in ("!UsersChoice!") do (
    endlocal
    goto MainMenu
)

rem Now it is safe to reference the variable value without usage of delayed
rem expansion as a syntax error caused by user input can't occur anymore.

rem The entered string does not contain any not expected character. But
rem it is possible that for example 11 was entered by mistake instead
rem of just 1. The entered string should have a length of 1 character.

if not "%UsersChoice:~1,1%" == "" (
    endlocal
    goto MainMenu
)

rem Exit this batch processing on user entered 0. Previous environment is
rem automatically restored by command processor by an implicit endlocal.

if "%UsersChoice%" == "0" exit /B

rem Restore previous environment as the called batch files are most
rem likely written for using standard command environment with delayed
rem expansion not enabled (exclamation mark interpreted different).
rem The current value of local environment variable must be passed
rem to previous environment for usage on GOTO command.

endlocal & goto Menu%UsersChoice%

:Menu1
call "%USERPROFILE%\Desktop\Menu1.bat"
goto MainMenu

:Menu2
call "%USERPROFILE%\Desktop\Menu2.bat"
goto MainMenu

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • cls /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • goto /?
  • rem /?
  • set /?
  • setlocal /?
  • title /?

For meaning of & in line endlocal & goto Menu%UsersChoice% see answer on Single line with multiple commands using Windows batch file.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
1

I tried your code and what I found was that when the input was 1 both :1 and :2 are executed but when the input is 2 only :2 is executed. To fix this you need to specify the end of :1 using Exit or another goto.

You might see that none the batches are being executed IF you do not put a pause in the end of your script. They would be executed but the result might just flash out of the screen.

Also I do not understand why have you used \\Menu1.batand not \Menu1.bat in

:1
call %userprofile%\desktop\\Menu1.bat

The final working code for me-

    @echo off
    :MENU
    title MENU0
    Echo 1 - Select Menu 1
    Echo 2 - Select Menu 2
    Echo 0 - Exit
    Echo.


    SET /P choice=Type the number or letter of task you want, then press ENTER:
    IF %choice%==1 GOTO 1
    IF %choice%==2 GOTO 2
    IF %choice%==0 EXIT

    :1
    call yourpathhere\Menu1.bat
    pause
    GOTO cont

    :2
    call whatsoever\Menu2.bat
    pause
    GOTO cont

    :cont
    exit

That should fix your problem. Hope I helped. Here's the working for 1st Menu

Here's the working for 2nd Menu

Rishav
  • 3,818
  • 1
  • 31
  • 49
0

I may not be a pro, but I could help you!

I always add extra code on my games in order to avoid bugs, like this:

set /p letter=

if %letter% == 1 goto nocheck1
if %letter% == 2 goto nocheck2
if %letter% == 3 exit

:nocheck1
if %letter% == 1 goto saves

:nocheck2
if %letter% == 2 goto howtoplay

Maybe it could work on your problem!

I might have the code to do it:

@echo off
cls

:menu
cls
echo 1. Open Batch 1
echo 2. Open Batch 2
set /p test=Enter number here -----> 

if %test% == 1 goto check1
if %test% == 2 goto check2

Edit the "Batch file name" text with your location of your batch file.

:check1
if %test% == 1 start C:\Users\%username%\Desktop\(batch file name).bat

:check2
if %test% == 2 start C:\Users\%username%\Desktop\(batch file name).bat

If there's still any errors with my code, let me know.

Hope this helps your problem!

0

Use cd to go to the location of batch file. For example:

rem myscript
echo calling batch file
cd demo\desktop\script
execute.bat
echo done

After the execution of that batch, control will return to the next line of your script.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
demo661
  • 11
  • 3
0

Use "Start" instead of "Call" like so,

@echo off
:MENU
title MENU0
Echo 1 - Select Menu 1
Echo 2 - Select Menu 2
Echo 0 - Exit
Echo.


SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1
IF %choice%==2 GOTO 2
IF %choice%==0 EXIT

:1
start %userprofile%\desktop\\Menu1.bat

:2
start %userprofile%\desktop\Menu2.bat
DELETED
  • 1
  • 4
0

Try This:

@echo off
:MENU
title MENU0
Echo 1 - Select Menu 1
Echo 2 - Select Menu 2
Echo 0 - Exit
Echo.


SET /P choice=Type the number or letter of task you want, then press
Enter:

IF %choice%==1 GOTO 1
IF %choice%==2 GOTO 2
IF %choice%==0 EXIT

:1
cd users
cd %userprofile%
cd desktop
:: call Menu1.bat or use: start Menu1.bat 
:: exit


:2
cd users
cd %userprofile%
cd desktop
:: call Menu2.bat or use: start Menu2.bat 
:: exit
DaveAmzon
  • 1
  • 2
-1
start "" C:\location\of\file\file.bat

This opens a new window, and as long as you have more commands to follow, the previous file that is calling the new one will still run along with this one.

Jordan B
  • 21
  • 2