2

我有一个读取 TXT 文件的 FOR 循环。并每 2 秒(ping 方法)逐行回显。当用户按下一个键时,我想退出这个循环(我也需要键值)。

这是我的代码:

@echo ofF
setlocal enabledelayedexpansion enableextensions
FOR /F "TOKENS=*" %%i IN (test.txt) DO ECHO %%i>x&FOR %%z IN (x) DO (cls&set /a length=%%~zz-1&del x&set $f=
                                                                                         for /l %%a in (0,1,!length!) do (set $f=Í!$f!)
                                                                                         echo É!$f!»&echo º %%i º&echo È!$f!¼
                                                                                         echo.&echo PRESS 1 - TO CONTINUE&PRESS X - TO ABORT
                                                                                         ping localhost -n 2 > NUL)

按键时我需要出去 文件 test.txt 如下所示:

感谢和抱歉我的英语不好

4

2 回答 2

4

这是相同的 npocmaka 的代码,但稍作修改:

@echo off

rem ---- set the value of your file here ----
set "file=D:\my file"

set options=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

for /f "usebackq delims=" %%L in ("%file%") do (
    echo %%L
    CHOICE /C %options% /cs  /D 0 /T 2 >nul
    if errorlevel 2 goto :endfor
)
:endfor
set /A choosen=%errorlevel%-1
color
setlocal enableDelayedExpansion
set pressed=!options:~%choosen%,1!
endlocal & set pressed=%pressed%
echo %pressed%
于 2013-05-23T20:06:18.627 回答
2

这将检测按下的数字和字母。0是默认按下的键,因此不会被检测到。这是我可以使用批处理文件实现的最佳效果。

@echo off

rem ---- set the path to your file here ----
set "file=file"

echo press a key to stop the print
for /f "usebackq tokens=* delims=*" %%L in ("%file%") do (
    setlocal DisableDelayedExpansion
    (echo(%%L)
    endlocal
    CHOICE /C abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 /cs  /D 0 /T 2 >nul
    if  not errorlevel 62 goto :endfor
)

:endfor

set /a choosen=%errorlevel%
color
setlocal enableDelayedExpansion
set /a counter=1
for %%# in (a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9 ) do (

    if !counter! NEQ %choosen% (
        set /a counter=!counter!+1
    ) else (
        set pressed=%%# & goto :endfor2
    )
)
:endfor2 
endlocal & set pressed=%pressed%
echo pressed button - %pressed% 
于 2013-05-23T19:13:05.573 回答