【问题标题】:Windows Command Prompt print text file with delayWindows 命令提示符延迟打印文本文件
【发布时间】:2015-07-30 11:33:29
【问题描述】:

我对命令提示符没有任何经验,但我想制作一个批处理脚本(为了好玩和学习),它会从给定位置打印文本文件行按行,有1秒延迟
我还希望它能够在我按下指定键(例如:空格)时暂停/取消暂停并为我提供额外的一行 (在那些已经编程运行的之上)当我按下另一个键(例如:enter)时。

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

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

【问题讨论】:

  • 使用for /ffind/n 显示会很容易,SO 上有很多类似的问题,但我不确定是否可以异步获取按键。
  • @wOxxOm - 这是可能的,但它非常复杂。

标签: windows batch-file command-prompt windows-console


【解决方案1】:

1) 如果你有编程经验,你会知道使用for 循环是最常见的逐一做事的方法,例如逐行

2)您可以简单地使用ping localhost -n 2 >nul延迟1秒,ping中的2不是表示2秒,而是1秒。 (我不知道,习惯就好)

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

4) 额外的行?嗯...记住 batch 不是一种强大的语言,所以...是的


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

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

【讨论】:

  • ping localhost -n 2 执行两次 ping 间隔 1 秒;你也可以使用timeout /t 1 /nobreak 等待一秒钟...
【解决方案2】:

您可以使用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 只允许 a-z、A-Z、0-9 和扩展字符 128-254。没有办法让它监听 EnterSpace。而choice 是我知道的唯一一个 Windows 命令,它会接受单个按键并根据按下的键执行一些有意义的操作。

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

【讨论】:

    【解决方案3】:

    “滚动编辑器”?这是一个疯狂的想法,不是吗?我喜欢! ;-) 我采纳了你的项目并补充几点...

    @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
    

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

    也许您可能对此similar project 感兴趣。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 2021-07-01
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      相关资源
      最近更新 更多