批处理文件如何并行读取两个文本文件?

阿希普

是否有一种简单且性能良好的方式来逐行并行读取两个(或更多)文本文件?因此,有一个循环可以在每次迭代中读取每个文本文件的一行。

for /F给定多个文件循环不能使用,因为这会一个接一个地读取文件。嵌套的这样的循环当然也没有任何意义。

阿希普

诀窍是使用标准输入 重定向 <(另见本网站)使用未定义的句柄(39)为文件读取整个代码块,命令set /P块中的实际读取线和0<&重定向未定义把手回至STDINset /P,从而供各行读取。

这是一个工作原理的示例:

假设有以下两个文本文件names.txt...:

Black
Blue
Green
Aqua
Red
Purple
Yellow
White
Grey
Brown

...和values.txt...:

0
1
2
3
4
5
6
7

...的目标是将它们逐行组合以实现此文件,names=values.txt...:

Black=0
Blue=1
Green=2
Aqua=3
Red=4
Purple=5
Yellow=6
White=7

...下面的代码实现了这一点(请参阅所有说明性注释,rem):

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem // Define constants here:
set "FILE1=names.txt"
set "FILE2=values.txt"
set "RET=names=values.txt" & rem // (none to output to console)
if not defined RET set "RET=con"

rem /* Count number of lines of 1st file (2nd file is not checked);
rem    this is necessary to know when to stop reading: */
for /F %%C in ('^< "%FILE1%" find /C /V ""') do set "NUM1=%%C"

rem /* Here input redirection is used, each file gets its individual
rem    (undefined) handle (that is not used by the system) which is later
rem    redirected to handle `0`, `STDIN`, in the parenthesised block;
rem    so the 1st file data stream is redirected to handle `4` and the
rem    2nd file to handle `3`; within the block, as soon as a line is read
rem    by `set /P` from a data stream, the respective handle is redirected
rem    back to `0`, `STDIN`, where `set /P` expects its input data: */
4< "%FILE1%" 3< "%FILE2%" > "%RET%" (
     rem // Loop through the number of lines of the 1st file:
     for /L %%I in (1,1,%NUM1%) do (
         set "LINE1=" & rem /* (clear variable to maintain empty lines;
                        rem     `set /P` does not change variable value
                        rem     in case nothing is entered/redirected) */
         rem // Change handle of 1st file back to `STDIN` and read line:
         0<&4 set /P "LINE1="
         set "LINE2=" & rem // (clear variable to maintain empty lines)
         rem // Change handle of 2nd file back to `STDIN` and read line:
         0<&3 set /P "LINE2="
         rem /* Return combined pair of lines (only if line of 2nd file is
         rem    not empty as `set /P` sets `ErrorLevel` on empty input): */
         if not ErrorLevel 1 echo(!LINE1!=!LINE2!
     )
)

endlocal
exit /B

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

批处理文件合并两个文本文件

批处理文件以从文本文件读取行

批处理文件以在两个文本文件中搜索通用字符串

批处理文件读取文本文件并尝试根据读取值停止进程

将文本文件的前两行复制到另一个文本文件的批处理文件

Windows批处理脚本来比较两个文本文件

如何合并两个文本文件中的行而不在批处理命令中创建换行符

使用批处理文件编辑文本文件

从文本文件读取值以在批处理文件变量中设置

如何创建一个批处理文件,该批处理文件将删除文本文件中的行

批处理文件:比较 2 个文本文件并更新文本文件中的特定行

如何使用批处理文件有选择地删除文本文件中的文本?

同时逐行读取两个文本文件

同时逐行读取两个文本文件

批处理文件:合并文本文件中的同名文本文件

如何逐行读取批处理文件中多余字符的文本文件?允许限制行长。(Windows,批处理脚本)

如何在文本文件中使用批处理文件变量?

如何从批处理文件创建空文本文件?

如何通过批处理文件从文本文件中删除不需要的行?

如何从批处理文件的文本文件中的行中删除多余的逗号?

如何使用Windows批处理文件拆分文本文件元素?

如何在Windows批处理文件中嵌入文本文件

如何在批处理文件中打印多个文本文件

如何在批处理文件上使用命令FOR获取文本文件的内容

使用批处理或perl脚本将两个文本文件合并为一个

如何将放入文本文件中的变量从批处理调用为批处理文件?

如何读取文本文件中的两个参数?

如何使用HTML5和Javascript同时读取两个文本文件

批处理文件可将文件从文本文件中的源移动到另一个文本文件中的多个文件夹