我没有任何关于命令提示符的经验,但是我想制作一个批处理脚本(为了好玩和学习),它会从给定位置打印文本文件,逐行, 1秒延迟 当我按下指定键(例如:空格)并向我提供额外行时,我还希望能够暂停/取消暂停当我按下另一个键时(在输入)时(在已编程运行的那些之上)。
我知道我可以通过ping localhost ping -n 5 127.0.0.1 > nul
来增加1秒的延迟
我知道我可以使用more text.txt
查看文本文件的内容,但我不知道如何遍历整个文本文件,直到满足EOF并且我不知道如何暂停/恢复并提供额外的线路。
希望它在这种情况下听起来不愚蠢或超出范围,但它只是我感兴趣的东西,我知道很多人都有这方面的知识。
答案 0 :(得分:0)
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
)
答案 1 :(得分:0)
您可以与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。没有办法让它听 Enter 或 Space 。 choice
是唯一一个Windows命令,我知道它会接受单个按键并根据按下的键执行一些有意义的操作。
我认为你必须使用某种编译语言(或者可能使用带有.NET类的PowerShell?)来监听控制台上的keypress events。您可以在JavaScript中执行此操作,但您必须在Web浏览器或HTA窗口中显示输出。
答案 2 :(得分:0)
A"滚动编辑器"?这是一个疯狂的想法,不是吗?我喜欢! ;-)
我采用了您的项目并添加了一些要点......
@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感兴趣。