0

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

4 回答 4

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);
于 2013-05-03T20:02:24.903 回答
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

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

于 2013-05-03T20:19:25.547 回答
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
于 2013-05-03T20:11:41.813 回答
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

这避免了额外的数组。

于 2013-05-03T20:24:30.783 回答