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

标签 windows batch-file cmd

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

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

最佳答案

诀窍是使用STDIN redirection < (另见 this site )使用未定义的句柄( 39 )用于文件读取的整个代码块,命令 set /P在 block 中实际读取一行和 0<&set /P 将未定义的句柄重定向回 STDIN , 因此要读取相应的行。

这是它如何工作的一个例子:

假设有以下两个文本文件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

关于windows - 批处理文件如何并行读取两个文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38214874/

相关文章:

c# - 为 Windows 编写我自己的自定义命令行 "wrapper"

batch-file - 创建一个批处理文件,在发生名称冲突时保留两个文件

batch-file - 如何清除批处理中的选定行而不是整个屏幕?

windows - 如何批量检查windows/explorer的启动时间?

windows - 批处理 - 将命令输出存储到变量(多行)

windows - 如何找到映射驱动器的可用空间百分比?

batch-file - 批处理文件将1个文件的内容复制到另一个文件中

c# - 如何在运行 exe 后关闭 cmd 窗口。从它的文件?

PHP exec() 不执行批处理文件

google-chrome - bat文件开始打开chrome chrome ://downloads/