本案例可将指定路径下所有txt格式文件合并为一个txt格式总文件,开发环境为vba,代码如下:(用户需将路径改为txt文件所在路径)
Sub union_txt_file()
Dim smallfile As String, resultfile As String
Dim a As String, b As String, path As String
path = "E:\练习"
mytime = Format(Now, "yymmdd_hh_mm_ss")
resultfile = "结果" & mytime & "_.txt"
resultfile_path = path & "\" & resultfile
smallfile = Dir(path & "\*.txt")
smallfile_fullname = path & "\" & smallfile
Open resultfile_path For Output As #1
Do While smallfile <> ""
If smallfile <> resultfile Then
Open smallfile_fullname For Input As #2
Do While Not EOF(2)
Line Input #2, a
b = b & a
Loop
Print #1, smallfile & vbCrLf
Print #1, b & vbCrLf
Close #2
End If
smallfile = Dir
Loop
Close #1
MsgBox "合并已完成" & Space(50) & vbCr & _
"vba代码二次开发qq:443440204", vbInformation, "版权所有qq:443440204"
End Sub