Close a batch file, but don't EXIT
Moderator: DosItHelp
Close a batch file, but don't EXIT
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?
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
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
Here's another version:
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.
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!
Reason: Please use CODE TAGS. Thanks!
Re: Close a batch file, but don't EXIT
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
I assume the issue here is, that the directory isn't changed (called the above test.bat):
Instead this should happen (test.2):
test2.bat:
With such a result:
(Indeed a curious result.)
penpen
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:\
test2.bat:
Code: Select all
@echo off
K:
cd \DVMS
Code: Select all
Z:\>test.bat
K:\dvms>
penpen
Re: Close a batch file, but don't EXIT
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):
penpen
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
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
:menu_3 Go to Programmer-Netcore Folder
endlocal
endlocal
K:
cd \DVMS
GOTO :End
Re: Close a batch file, but don't EXIT
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
Thanks Squashman.
sdear, try this line 3 below.
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
Well, i can't see your result, so i have to guess: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
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
Code: Select all
if "%choice%" == "3" goto :menu_3
penpen
Re: Close a batch file, but don't EXIT
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.
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.