Home > Mobile >  How can I interrupt a loop in CMD batch file using a specific key input?
How can I interrupt a loop in CMD batch file using a specific key input?

Time:12-23

here is the whole idea: I have a menu to test ping different DNS Servers in a batch file just like below:

:MainMenu
ECHO 1. DNS Server 1
ECHO 2. DNS Server 2

Now when the user chooses one of the options above, the process starts to ping the selected server in this case I use google DNS 8.8.8.8 :

:loopStarter
ECHO ***AUTO PING MODE IS ENABLED***
ECHO Pinging...
ping 8.8.8.8

CLS

GOTO loopStarter

As you can see this bunch of code creates a loop and pings the server and never stops,

HOWEVER

I want to stop the loop by entering a key (any key) to take me back to the :MainMenu

===========

I have seen and tested many answers like Here but unfortunately either I don't understand the code or it is not related to my problem.

I would appreciate it if anyone could guide me through.

CodePudding user response:

A simple parallel thread example.
The :key_detector thread waits for a key, then it deletes the notification_file.
The :ping_test executes a ping, then it checks the existence of the notification_file, if it's still exists the loop will be repeated.

The threads are started by the two child instances of a pipe.
This looks a bit complicated, because you can't call a label directly inside a pipe, it just starts the own program and uses the trampoline at the beginning to jump to the label.

@echo off
REM *** This is a trampoline to jump to a function when a child process shall be invoked
for /F "tokens=3 delims=:" %%L in ("%~0") do goto %%L

set "notification_file=%temp%\keypress.tmp"

echo dummy > "%notification_file%"
call "%~d0\:key_detector:\..%~pnx0" | call "%~d0\:ping_test:\..%~pnx0"
exit /b


:ping_test
ping -n 2 8.8.8.8 
if not exist "%notification_file%" exit /b
goto :ping_test

:key_detector
for /F "tokens=1 skip=1 eol=" %%C in ('"replace /w ? . < con"') do @(
    set "key=%%C"
    del %notification_file%
)

exit /b

CodePudding user response:

Based on this method found here to create a dynamic menu, you can create something like that :


@echo off
:menuLOOP
Title Pinging some DNS
Color 9E & Mode 80,15
::===========================================================================
echo(
echo(
echo(       ***************************** Menu ******************************
echo(
@for /f "tokens=2* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do (
echo            %%A  %%B)
echo(
echo(       *****************************************************************
echo( Make A Selection And Hit ENTER to Ping or simply hit ENTER to quit:
Set /p Selection= || GOTO :EOF
echo( & call :menu_[%Selection%]
GOTO:menuLOOP
::===========================================================================
:menu_[1] Ping DNS Server 1
Cls
ECHO( Pinging...
ping 8.8.8.8
echo( & echo( Type any key to return to the Main Menu
pause>nul
Goto:menuLoop
::---------------------------------------------------------------------------
:menu_[2] Ping DNS Server 2
Cls
ECHO( Pinging...
ping 8.8.4.4
echo( & echo( Type any key to return to the Main Menu
pause>nul
Goto:menuLoop
::---------------------------------------------------------------------------
  • Related