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



我需要一个批处理文件来组合两个文本文件。我的文件是:

file1.txt
1
2
3
4
file2.txt
A
B
C
D
E
F
G
H
I
J
K
L

我需要得到看起来像这个的混合文件

mix.txt
1
A
B
C
D
2
E
F
G
H
3
I
J
K
L

注意:对于文件1的每一行,我需要文件2中的4行。1->1-4,2->5-8、3->9-12

我尝试了这个程序(bat文件解决方案(,但不知道如何修改它以获得符合我要求的结果。

@echo off
set f1=file1.txt
set f2=file2.txt
set outfile=mix.txt
type nul>%outfile%
(
for /f "delims=" %%a in (%f1%) do (
setlocal enabledelayedexpansion
set /p line=
echo(%%a!line!>>%outfile%
endlocal
)
)<%f2%
pause

您将需要某种行计数器:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Improved quoted `set` syntax:
set "f1=file1.txt"
set "f2=file2.txt"
set "outfile=mix.txt"
set "quotient=4"
rem // Redirect the whole block once:
< "%f1%" > "%outfile%" (
rem // Temporarily precede each line with a line number:
for /f "delims=" %%a in ('findstr /N "^" "%f2%"') do (
rem // Get modulo of the line number:
set "item=%%a" & set /A "num=(item+quotient-1)%%quotient"
setlocal EnableDelayedExpansion
rem // Return a line from the other file only if modulo is zero:
if !num! equ 0 (
set "line=" & set /P line=""
echo(!line!
)
rem // Return current line with the line number prefix removed:
echo(!item:*:=!
endlocal
)
)
endlocal
pause

file2.txt的长度决定了发生多少次迭代。

最新更新