回显文本直到按下某个键

时间:2013-05-23 18:48:09

标签: batch-file

我有一个FOR循环读取TXT文件。并且逐行回复它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如下所示:

谢谢,抱歉我的英语不好

2 个答案:

答案 0 :(得分: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%

答案 1 :(得分: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%