這是整個想法:我有一個選單可以在批處理檔案中測驗 ping 不同的 DNS 服務器,如下所示:
:MainMenu
ECHO 1. DNS Server 1
ECHO 2. DNS Server 2
現在,當用戶選擇上述選項之一時,行程開始到ping選定的服務器,在這種情況下我使用 google DNS 8.8.8.8:
:loopStarter
ECHO ***AUTO PING MODE IS ENABLED***
ECHO Pinging...
ping 8.8.8.8
CLS
GOTO loopStarter
正如你所看到的,這堆代碼創建了一個回圈和pings服務器并且永不停止,
然而
我想通過輸入一個鍵(任意鍵)將我帶回 :MainMenu
============
我已經看到并測驗了許多像Here這樣的答案,但不幸的是我不理解代碼或者它與我的問題無關。
如果有人能指導我完成,我將不勝感激。
uj5u.com熱心網友回復:
一個簡單的并行執行緒示例。
:key_detector 執行緒等待一個密鑰,然后洗掉notification_file。
:ping_test 執行 ping,然后檢查notification_file是否存在,如果它仍然存在,回圈將重復。
執行緒由管道的兩個子實體啟動。
這看起來有點復雜,因為不能直接在管道內部呼叫標簽,它只是啟動自己的程式,并使用開頭的蹦床跳轉到標簽。
@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
uj5u.com熱心網友回復:
基于這里找到的創建動態選單的方法,您可以創建類似的內容:
@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
::---------------------------------------------------------------------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/392201.html
上一篇:命令輸出設定為變數
