Page 1 of 1

Close a batch file, but don't EXIT

Posted: 18 Apr 2014 10:39
by sdear
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?

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

Posted: 18 Apr 2014 10:45
by foxidrive
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

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

Posted: 18 Apr 2014 13:54
by sdear
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.

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

Posted: 18 Apr 2014 14:20
by Squashman
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

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

Posted: 18 Apr 2014 15:02
by penpen
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

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

Posted: 18 Apr 2014 15:16
by penpen
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

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

Posted: 21 Apr 2014 09:00
by sdear
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

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

Posted: 21 Apr 2014 09:22
by Squashman
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.

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

Posted: 21 Apr 2014 09:26
by foxidrive
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

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

Posted: 21 Apr 2014 09:36
by penpen
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

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

Posted: 21 Apr 2014 09:46
by sdear
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.