Close a batch file, but don't EXIT

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sdear
Posts: 4
Joined: 18 Apr 2014 09:59

Close a batch file, but don't EXIT

#1 Post by sdear » 18 Apr 2014 10:39

I'm using Windows 7.
I found the below batch file on your site; thank you.
I've used it to consolidate my other batch files & they all work fine.
The part below stays stuck in the menu & I need to work on the other directory.

@ECHO OFF
REM.-- Prepare the Command Processor
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

:menuLOOP
cls
echo.
echo.= Menu =========================================
echo.
for /f "tokens=1,2,* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo. %%B %%C
set choice=
echo.&set /p choice=Make a choice or hit ENTER to quit: ||GOTO:EOF
echo.&call:menu_%choice%
GOTO:menuLOOP

:menu_3 Go to Programmer-Netcore Folder
K:
cd \DVMS
GOTO End


:End
'"prove" it got here
echo This is the end
pause > nul

How do I get out of the batch file, but not out of DOS?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Close a batch file, but don't EXIT

#2 Post by foxidrive » 18 Apr 2014 10:45

Here's a snippet:

Code: Select all

set choice=
echo.
set /p "choice=Make a choice or hit ENTER to quit: "
echo.
if not defined choice goto :EOF
call :menu_%choice%
GOTO :menuLOOP

sdear
Posts: 4
Joined: 18 Apr 2014 09:59

Re: Close a batch file, but don't EXIT

#3 Post by sdear » 18 Apr 2014 13:54

Here's another version:

Code: Select all

@ECHO OFF
REM.-- Prepare the Command Processor
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

:menuLOOP
cls
echo.
echo.= Menu =========================================
echo.
for /f "tokens=1,2,* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo.  %%B  %%C
set choice=
echo.
set /p "choice=Make a choice or hit ENTER to quit: "
echo.
if not defined choice goto :EOF
call :menu_%choice%
GOTO :menuLOOP

:menu_3   Go to Programmer-Netcore Folder
K:
cd \DVMS
GOTO End

:menu_9   Ping my computer
cls
Call G.bat
echo Hit any key to continue
pause > nul
GOTO:EOF

:End
echo This is the end
pause > nul


When I choose 9 it works correctly, however 3 goes to :End
but then returns to the menu.
I need to quit the batch file & go to K:\DVMS as :menu_3 is trying to do.
Last edited by Squashman on 18 Apr 2014 14:17, edited 1 time in total.
Reason: Please use CODE TAGS. Thanks!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Close a batch file, but don't EXIT

#4 Post by Squashman » 18 Apr 2014 14:20

sdear wrote:
When I choose 9 it works correctly, however 3 goes to :End
but then returns to the menu.
I need to quit the batch file & go to K:\DVMS as :menu_3 is trying to do.

Of course Menu 3 goes to the END. You are telling it to do that.

Code: Select all

:menu_3   Go to Programmer-Netcore Folder
K:
cd \DVMS
GOTO End

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Close a batch file, but don't EXIT

#5 Post by penpen » 18 Apr 2014 15:02

I assume the issue here is, that the directory isn't changed (called the above test.bat):

Code: Select all

Z:\test.bat


= Menu =========================================

  3  Go to Programmer-Netcore Folder
  9  Ping my computer

Make a choice or hit ENTER to quit: 3

This is the end

= Menu =========================================

  3  Go to Programmer-Netcore Folder
  9  Ping my computer

Make a choice or hit ENTER to quit:

Z:\
Instead this should happen (test.2):
test2.bat:

Code: Select all

@echo off
K:
cd \DVMS
With such a result:

Code: Select all

Z:\>test.bat

K:\dvms>
(Indeed a curious result.)

penpen

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Close a batch file, but don't EXIT

#6 Post by penpen » 18 Apr 2014 15:16

The problem was caused by the local environment variable blocks created using "setlocal" and "call": The (dynamic) variable storing the actual path (CD) is overwritten with the old value again, when returning from call or setlocal, so the path seemed to be not changed.
So you have to avoid it, for example like this (basing on what you want to do in this menue this might be very suboptimal):

Code: Select all

@ECHO OFF
REM.-- Prepare the Command Processor
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

:menuLOOP
cls
echo.
echo.= Menu =========================================
echo.
for /f "tokens=1,2,* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo.  %%B  %%C
set choice=
echo.
set /p "choice=Make a choice or hit ENTER to quit: "
echo.
if not defined choice goto :EOF
if "%choice%" == "3" goto :menu_3
call :menu_%choice%
GOTO :menuLOOP

:menu_3   Go to Programmer-Netcore Folder
endlocal
endlocal
K:
cd \DVMS
:: instead of the next 5 lines you might want to use "GOTO :End" instead
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
echo This is the end
pause > nul
GOTO :menuLOOP

:menu_9   Ping my computer
cls
Call G.bat
echo Hit any key to continue
pause > nul
GOTO:EOF

:End
echo This is the end
pause > nul


penpen

sdear
Posts: 4
Joined: 18 Apr 2014 09:59

Re: Close a batch file, but don't EXIT

#7 Post by sdear » 21 Apr 2014 09:00

I get the same results whether I use the version suggested or this shorter version.

:menu_3 Go to Programmer-Netcore Folder
endlocal
endlocal
K:
cd \DVMS
GOTO :End

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Close a batch file, but don't EXIT

#8 Post by Squashman » 21 Apr 2014 09:22

foxidrive wrote:What do you expect option 3 to do?


I think he is expecting the batch file to exit and keep the cmd window open.

I think you would have to open a separate CMD window to do that.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Close a batch file, but don't EXIT

#9 Post by foxidrive » 21 Apr 2014 09:26

Thanks Squashman.


sdear, try this line 3 below.

Code: Select all

set /p "choice=Make a choice or hit ENTER to quit: "
echo.
if "%choice%"=="3" exit /b
if not defined choice goto :EOF
call :menu_%choice%
GOTO :menuLOOP

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Close a batch file, but don't EXIT

#10 Post by penpen » 21 Apr 2014 09:36

sdear wrote:I get the same results whether I use the version suggested or this shorter version.

:menu_3 Go to Programmer-Netcore Folder
endlocal
endlocal
K:
cd \DVMS
GOTO :End
Well, i can't see your result, so i have to guess:
Maybe you have only copy pasted/changed these lines:

Code: Select all

:menu_3   Go to Programmer-Netcore Folder
endlocal
endlocal
K:
cd \DVMS
:: instead of the next 5 lines you might want to use "GOTO :End" instead
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
echo This is the end
pause > nul
GOTO :menuLOOP
and you may have forgaotten to add this line:

Code: Select all

if "%choice%" == "3" goto :menu_3
If i have guessed wrong, you should post, what happens (for example you may rem out the "cls" line and copy paste the screen content).

penpen

sdear
Posts: 4
Joined: 18 Apr 2014 09:59

Re: Close a batch file, but don't EXIT

#11 Post by sdear » 21 Apr 2014 09:46

Yes, I had a brain cramp: I only took the menu_3 code & just now noticed that I'd missed this:
if "%choice%" == "3" goto :menu_3

I'm assuming I could also use this if I had a similar menu item (so something outside the menu)...

Thank for the help, guys.
I haven't done much batch programming since DOS 6.

Post Reply