如何逐行读取和打印文本文件的内容? [英] How to read and print contents of text file line by line?

查看:124
本文介绍了如何逐行读取和打印文本文件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我不知道如何一次从一个* .txt文本文件中获取CMD回显行,并且要稍加延迟才能使其看起来像正在处理.

So, I have no clue on how to have CMD echo lines from a *.txt text file one at a time with a tiny delay to make it seem like it's processing.

单批处理是否有可能?

我已经尝试进行研究,但是我找不到足够的文本操作来做到这一点,但是我确实知道如何在每个命令之间进行暂停以及如何执行循环.

I've tried doing research, but I can't find sufficient text manipulation to be able to do this, but I do know how to make a pause between each command and how to do loops.

推荐答案

让我们假设文本文件TestFile.txt应该逐行输出,这是ANSI编码的文本文件,仅包含此文本的ASCII字符:

Let us assume the text file TestFile.txt should be output line by line which is an ANSI encoded text file with just ASCII characters containing this text:

Line 1 is with nothing special. Next line 2 is an empty line.

;Line 3 with a semicolon at beginning.
   Line 4 has leading spaces.
    Line 5 has a leading horizontal tab.
Line 6 is with nothing special. Next line 7 has just a tab and four spaces if used internet browser does not remove them.
        
Line 8 is ! with exclamation marks ! in line!
? Line 9 starts with a question mark.
: Line 10 starts with a colon.
] Line 11 starts with a closing square bracket.

下面的批处理文件逐行输出此文本文件,每行之间有一秒钟的延迟,但第二行完全为空.

The batch file below outputs this text file line by line with one second delay between each line with the exception of second line which is completely empty.

@echo off
title Read line by line with delay
setlocal EnableExtensions DisableDelayedExpansion

rem Use command TIMEOUT by default for 1 second delay. But use
rem PING in case of TIMEOUT does not exist as on Windows XP.
set "DelayCommand=%SystemRoot%\System32\timeout.exe /T 1 /NOBREAK"
if not exist %SystemRoot%\System32\timeout.exe set "DelayCommand=%SystemRoot%\System32\ping.exe 127.0.0.1 -n 2"

for /F "usebackq eol=¿ delims=" %%I in ("TestFile.txt") do (
    echo(%%I
    %DelayCommand% >nul
)
endlocal
pause

eol=之后看起来很奇怪的字符¿是带有十六进制Unicode值00BF的倒置问号,用于输出正确的第三行.由于重新定义了行尾字符,因此不会输出开头带有问号的行.

The strange looking character ¿ after eol= is an inverted question mark with hexadecimal Unicode value 00BF used to output third line correct. A line with an inverted question mark at beginning would not be output because of this redefinition of end of line character.

此批处理文件代码并非旨在输出任何类型的字符编码取决于哪个字符包含文本文件. Windows命令行环境不适用于任何文本文件的输出.

This batch file code is not designed to output any type of text file with any type of character encoding independent on which characters contains the text file. The Windows command line environment is not designed for output of any text file.

还可以使用不同的,不带引号的语法来指定 FOR 选项delimseolusebackq,以定义分隔符的空列表strong>没有换行符:

It is also possible to use a different, unquoted syntax to specify the FOR options delims, eol and usebackq to define an empty list of delimiters and no end of line character:

@echo off
title Read line by line with delay
setlocal EnableExtensions DisableDelayedExpansion

rem Use command TIMEOUT by default for 1 second delay. But use
rem PING in case of TIMEOUT does not exist as on Windows XP.
set "DelayCommand=%SystemRoot%\System32\timeout.exe /T 1 /NOBREAK"
if not exist %SystemRoot%\System32\timeout.exe set "DelayCommand=%SystemRoot%\System32\ping.exe 127.0.0.1 -n 2"

for /F usebackq^ delims^=^ eol^= %%I in ("TestFile.txt") do (
    echo(%%I
    %DelayCommand% >nul
)
endlocal
pause

在使用转义的情况下,请转到 aschipfl ,以获得这三个 FOR 选项的替代语法字符^可以在不带双引号的选项字符串中转义等号和空格,以被cmd.exe字符串usebackq delims= eol=解释为for /F一个自变量字符串.

Thanks goes to aschipfl for this alternate syntax of the three FOR options with using escape character ^ to escape the equal signs and spaces in not double quoted options string to get interpreted by cmd.exe the string usebackq delims= eol= as one argument string for for /F.

这里有(而不是通常用来输出也只用制表符和一些常规空格的正确第7行的空格.另请参阅DosTips论坛主题 ECHO.无法输入文本或空白行-而是使用ECHO/. echo/%%I不能更正以问号开头的输出行9.

There is ( instead of a space as usually used to output also correct line 7 with just a tab and some normal spaces. See also DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/. echo/%%I does not correct output line 9 starting with a question mark.

无法使用 FOR 不忽略空行的选项进行定义.但是可以使用 FIND FINDSTR 输出文本文件,该文本文件的所有行开头都带有行号,因此不再有空行.行号括在方括号(FIND)中,或与冒号(FINDSTR)分隔.可以仅在行号之后的]:的第一个序列之后的字符串分配给循环变量,这在大多数情况下意味着整行与文本文件中的一样.但是,如果文本文件中的一行偶然以]:开头,则 FOR 也会删除该定界符.解决方法是以下代码:

It is not possible to define with an option that FOR does not ignore empty lines. But it is possible with FIND or FINDSTR to output a text file with all lines with a line number at beginning and so having no empty line anymore. The line number is enclosed in square brackets (FIND) or separated with a colon (FINDSTR) from rest of the line. It would be possible to assign to loop variable only the string after first sequence of ] or : after line number which in most cases means the entire line as in text file. But if a line in text file starts by chance with ] or :, FOR would remove this delimiter character too. The solution is this code:

@echo off
title Read line by line with delay
setlocal EnableExtensions DisableDelayedExpansion

rem Use command TIMEOUT by default for 1 second delay. But use
rem PING in case of TIMEOUT does not exist as on Windows XP.
set "DelayCommand=%SystemRoot%\System32\timeout.exe /T 1 /NOBREAK"
if not exist %SystemRoot%\System32\timeout.exe set "DelayCommand=%SystemRoot%\System32\ping.exe 127.0.0.1 -n 2"

for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "TestFile.txt" 2^>nul') do (
    set "Line=%%I"
    setlocal EnableDelayedExpansion
    echo(!Line:*:=!
    endlocal
    %DelayCommand% >nul
)
endlocal
pause

FINDSTR 输出的带有行号和冒号的整行在 FOR %ComSpec% /c'内的命令行为其他参数已分配给循环变量I,该变量紧随环境变量Line分配.

The entire line with line number and colon output by FINDSTR executed in a separate command processes started by FOR with %ComSpec% /c and the command line within ' as additional arguments is assigned to loop variable I which is assigned next to environment variable Line.

然后根据需要在下一行启用延迟扩展,这会导致在创建当前环境变量的副本之前将当前环境变量列表的地址以及当前目录路径,命令扩展状态和延迟扩展状态推送到堆栈列表.

Then delayed expansion is enabled as needed for next line which results in pushing address of current environment variables list on stack as well as current directory path, state of command extensions and state of delayed expansion before creating a copy of the current environment variables list.

接下来输出环境变量Line的值,但用直到第一个冒号代替所有内容,这将导致输出的真实行存储在文本文件中,而没有行号并在 FINDSTR 开头插入冒号.

Next the value of environment variable Line is output, but with substituting everything up to first colon by nothing which results in the output of the real line as stored in text file without the line number and the colon inserted at beginning by FINDSTR.

最后,从内存中删除创建的环境变量列表的副本,并从堆栈中弹出并设置延迟扩展和命令扩展的先前状态,并将当前目录路径再次设置为环境变量的当前目录和先前地址还原列表以还原环境变量列表.

Finally the created copy of environment variables list is deleted from memory, and previous states of delayed expansion and command extension are popped from stack and set as well as the current directory path is set again as current directory and previous address of environment variables list is restored to restore the list of environment variables.

对于文本文件中的每一行运行命令setlocal EnableDelayedExpansionendlocal当然不仅仅效率很高,它不仅可以启用/禁用延迟扩展,而且在这里要获得带有感叹号的行是必须的更正分配给环境变量Line的值,然后处理下一步更正Line的值.由于每条线的输出之间存在一秒的延迟,因此效率损失在这里并不是真正的问题.

It is of course not very efficient to run for each line in text file the commands setlocal EnableDelayedExpansion and endlocal doing much more than just enabling/disabling delayed expansion, but this is necessary here to get lines with an exclamation mark correct assigned to environment variable Line and process next correct the value of Line. The efficiency loss is not really problematic here because of the delay of one second between output of each line.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • if /?
  • ping /?
  • rem /?
  • set /?
  • setlocal /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • if /?
  • ping /?
  • rem /?
  • set /?
  • setlocal /?

这篇关于如何逐行读取和打印文本文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆