繁体   English   中英

Windows命令提示符延迟打印文本文件

[英]Windows Command Prompt print text file with delay

我对命令提示符没有任何经验,但是我想制作一个批处理脚本(为了好玩和学习),它将在给定位置逐行 打印文本文件延迟1秒
我还希望它能够在我按指定的键(例如: space )时暂停/取消暂停 ,并在我按下另一个键(例如: enter )时向​​我多加一行 (在已编程运行的代码的顶部)。

我知道我可以通过ping localhost ping -n 5 127.0.0.1 > nul添加1秒的延迟
而且我知道我可以使用more text.txt来查看文本文件的内容,但是我不知道如何遍历整个文本文件,直到遇到EOF为止,而且我不知道如何暂停/恢复和提供额外的内容线。

希望在这种情况下听起来不会很愚蠢或超出范围,但这只是我很感兴趣的事情,而且我知道这里很多人都有这样做的知识。

1)如果您有编程经验,就会知道使用for循环是一种最常见的方式,一个接一个地做事,例如逐行地做。

2)您可以简单地使用ping localhost -n 2 >nul延迟1秒,而ping中的2并不表示2秒,而是1秒。 (我对此一无所知,请习惯一下)

3)当cmd ping时,您不能暂停/取消暂停,我的意思是没有办法强制程序暂停/取消暂停,因为延迟过程只在一行代码中执行! 或者您可以神奇地向其中添加一些代码,例如ping localhost -n 2 pause while(KeyDown(SPACE)) >nul (只是在开玩笑))

4)多余的线? 嗯...记住批处理不是一种强大的语言,所以...是的


这是一个简单的代码,用于每秒在.txt文件中逐行打印文本

for /f %%a in (your_text.txt) do (
    echo %%a
    ping localhost -n 2 >nul
)

您可以choice /t 1 (超时1秒)和除空格键以外的其他来同步执行此操作。 也许P代表暂停?

@echo off
setlocal

set "textfile=notes.txt"

echo Hit P to pause / resume, Q to quit.
echo;

for /f "tokens=1* delims=:" %%I in ('findstr /n "^" "%textfile%"') do (
    echo(%%J
    choice /t 1 /c €pq /d € >NUL
    if errorlevel 3 exit /b 0
    if errorlevel 2 (
        pause >NUL
        ping -n 1 -w 750 169.254.1.1 >NUL
    )
)

exit /b 0

不幸的是, choice只允许使用az,AZ,0-9和扩展字符128-254。 无法使其监听EnterSpace 而且choice是我知道的唯一Windows命令,它将接受一次按键并根据所按下的按键执行有意义的操作。

我认为您必须使用某种编译语言(或者可能是带有.NET类的PowerShell?)来侦听控制台上的按键事件 您可能可以使用JavaScript进行此操作,但必须在Web浏览器或HTA窗口中显示输出。

“滚动编辑器”? 这是一个疯狂的主意,不是吗? 我喜欢! ;-)我通过了您的项目并添加了一些要点...

@echo off

rem ScrollEditor.bat: "dynamic" very simple line editor
rem Antonio Perez Ayala aka Aacini

if "%~1" neq "" if "%~1" neq "/?" goto begin

echo ScrollEditor.bat filename.ext
echo/
echo File lines will be continually scrolling, one per second.
echo/
echo You may pause the scroll via P key. In the "paused" state, the last displayed
echo line is named "current line", and the following commands are active:
echo/
echo    #L     Return/advance the listing to line #; continue the scroll from there.
echo    [#]D   Delete [from previous line # up to] current line.
echo    I      Insert lines after current line; end insert with *two* empty lines.
echo    P      End "paused" state; continue the scroll from current line on.
echo    E      End edit and save file, keep original file with .bak extension.
echo    Q      Quit edit, not save file.
goto :EOF

:begin
if not exist %1 echo File not found & goto :EOF

rem Load file lines into "line" array
set /P "=Loading file... " < NUL
setlocal DisableDelayedExpansion
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %1') do (
   set "line[%%a]=%%b"
   set "lastLine=%%a"
)
echo last line: %lastLine%
echo To pause scrolling, press: P
echo/
setlocal EnableDelayedExpansion

set "validCommands=LDIPEQ"
set currentLine=1
:command-P  End "paused" state
:ScrollLine
   if %currentLine% gtr %lastLine% (
      set "currentLine=%lastLine%"
      echo  EOF
      goto GetCommand
   )
   set "num=   %currentLine%"
   echo %num:~-4%: !line[%currentLine%]!
   set /A currentLine+=1
   choice /C PC /N /T 1 /D C >NUL
if errorlevel 2 goto ScrollLine

rem Enter paused state
set /A currentLine-=1
:GetCommand
echo/
set /P "command=Command [#L,#D,I,P,E,Q]? "
set "letter=%command:~-1%"
if "!validCommands:%letter%=!" equ "%validCommands%" goto GetCommand
goto command-%letter%

:command-L  Go to line #; continue scrolling
set "currentLine=%command:~0,-1%"
goto ScrollLine


:command-D  Delete from line # to current line
set "prevLine=%command:~0,-1%"
if not defined prevLine set "prevLine=%currentLine%"

rem Move lines after last deleted one into deleted lines
set /A currentLine+=1, newCurrent=prevLine-1, lines=currentLine-prevLine
for /L %%j in (%currentLine%,1,%lastLine%) do (
   set "line[!prevLine!]=!line[%%j]!"
   set /A prevLine+=1
)
set /A currentLine=newCurrent, lastLine=prevLine-1
if %currentLine% equ 0 set "currentLine=1"
echo %lines% line(s) deleted (current=%currentLine%, last=%lastLine%)
goto GetCommand


:command-I  Insert lines after current one
echo End insert with *two* empty lines
echo/

rem Read new lines into "ins" array
set "newLine=%currentLine%"
:insertLine
   set "line="
   set /A newLine+=1
   set "num=  %newLine%"
   set /P "line=+%num:~-3%: "
   set "ins[%newLine%]=!line!"
   rem The most complex part: end in two empty lines...
   if not defined line (
      set /A newLine+=1
      set "num=  !newLine!"
      set /P "line=+!num:~-3!: "
      if defined line (
         set "ins[!newLine!]=!line!"
      ) else (
         set /A newLine-=2
      )
   )
if defined line goto insertLine

rem Move old lines to new place to make room for new lines
set /A lines=newLine-currentLine, currentLine+=1, newLast=lastLine+lines
for /L %%j in (%lastLine%,-1,%currentLine%) do (
   set "line[!newLast!]=!line[%%j]!"
   set /A newLast-=1
)

rem Insert new lines in old place
for /L %%j in (%currentLine%,1,%newLine%) do set "line[%%j]=!ins[%%j]!"
set /A lastLine+=lines, currentLine=newLine
echo %lines% line(s) inserted (current=%currentLine%, last=%lastLine%)
goto GetCommand


:command-E  End edit, save file
echo Saving file...
move /Y %1 "%~N1.bak"
(for /L %%i in (1,1,%lastLine%) do echo(!line[%%i]!) > %1

:command-Q  Quit edit
echo End edit

该程序存在多个问题:不要检查命令中的有效输入,可能会遇到特殊批处理字符的问题,并且如果一行的第一个字符是冒号,则应将其消除。 但是,这是该项目的良好起点!

也许您可能对此类似项目感兴趣。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM