我想使用平面参考文件将多个文件合并为一个文件。我的平面文件如下所示:
master_A | SubA | SubSubA
master_A | SubA | SubSubB
master_A | SubA | SubSubC
master_A | SubB | SubSubA
master_B | SubC | SubSubA
master_B | SubC | SubSubB
...
B和C组合在磁盘上定义我的文件位置,我想使用此流程根据A列(主控)的分组值生成单个文件。
换句话说,上面的示例将导致2个文件,并且将添加子文件。所以流程如下:
生成一个名为master_A.txt的文件,并添加SubA_SubSubA.txt,SubA_SubSubB.txt,SubA_SubSubC.txt和SubB_SubSubA.txt的内容。
接下来,生成master_B并添加SubC_SubSubA.txt和SubC_SubSubB.txt的内容
继续直到参考文件结束......
说实话,我没有比下面更进一步......
for /F "tokens=1,2,3" %%i in (myFlatFile.txt) do (
set myMaster=%%i
set myFile=%%j_%%k.txt
rem if myMaster differs from the previous one generate a new file using myMaster
rem as filename, then add myFile content untill the master changes again.
rem What would be the best way to achieve this ?
)
答案 0 :(得分:0)
@echo off
for /F "tokens=1-3 delims=| " %%i in (myFlatFile.txt) do (
type %%j_%%k.txt >> %%i.txt
)
以前代码的唯一问题是如果master ...文件可能在程序运行之前存在。在这种情况下,先前的文件删除周期可以解决问题。