连接2个文本文件的每一行

时间:2013-05-03 19:49:57

标签: string vbscript batch-file string-concatenation

File1.txt是:

Abcd
Efghhh
Ijkl
+1000 other lines

File2.txt是:

1234
1368
QL23372
+1000 other lines

我想创建包含以下内容的File3.txt:

Abcd$1234
Efghhh$1368
Ijkl$QL23372
+1000 other lines

如何在批处理或VBscript中执行此操作?

4 个答案:

答案 0 :(得分:1)

假设两个文件的行数相同:

string[] lines1 = File.ReadAllLines("File1.txt");
string[] lines2 = File.ReadAllLines("File2.txt");

string[] lines3 = new string[lines1.Length];
for(int i=0;i<lines1.Length;i++)
    lines3[i]=lines1[i]+"$"+lines2[i];
File.WriteAllLines("File3.txt",lines3);

答案 1 :(得分:1)

@echo off
setlocal EnableDelayedExpansion

rem Read File1.txt with SET /P command via standard input
< File1.txt (
   rem Read File2.txt with FOR command
   for /F "delims=" %%a in (File2.txt) do (
      set /P line1=
      echo !line1!$%%a
   )
rem Send output to File3.txt
) > File3.txt

如果输入文件包含空行,则此Batch程序将失败。它也会因特殊的批处理字符失败,例如! < | >。如果需要,可以修复这些限制。

答案 2 :(得分:0)

使用* .bat文件的解决方案:

setlocal EnableDelayedExpansion

set i=0
for /f "delims=" %%A in (file1.txt) do (
    set /A i+=1
    set arr1[!i!]=%%A
)

set i=0
for /f "delims=" %%A in (file2.txt) do (
    set /A i+=1
    set arr2[!i!]=%%A
)

for /L %%i in (1,1,%i%) do @echo !arr1[%%i]!$!arr2[%%i]!>> result.txt

答案 3 :(得分:0)

VBScript尝试:

  Dim oFS   : Set oFS   = CreateObject("Scripting.FileSystemObject")
  Dim tsFst : Set tsFst = oFS.OpenTextFile("M:\lib\kurs0705\testdata\filezipper\fst")
  Dim tsSnd : Set tsSnd = oFS.OpenTextFile("M:\lib\kurs0705\testdata\filezipper\snd")
  Do Until tsFst.AtEndOfStream Or tsSnd.AtEndOfStream
     WScript.Echo tsFst.ReadLine() & "$" & tsSnd.ReadLine()
  Loop
  tsSnd.Close
  tsFst.Close

避免额外的数组。