其实方法有很多,我就列举常用的两个吧...
方法一:
循环获得文件夹里的文件;
判断是否为文本文件;
按行读取文本文件;。。。
下面我用Dir()函数获得文件夹里的文件,文件将按“文件名”排序。也可以结合文件系统来做。
Private Sub Command1_Click()
Dim FolderPath As String, FileName As String, fnum1 As Integer, fnum2 As Integer
Dim sL As String
FolderPath = "D:\123\"
fnum1 = FreeFile
Open FolderPath & "tmp.txt" For Output As fnum1
FileName = Dir(FolderPath)
Do Until FileName = ""
If LCase(Right(FileName, 4)) = ".txt" And FileName <> "tmp.txt" Then
fnum2 = FreeFile
Open FolderPath & FileName For Input As fnum2
Do Until EOF(fnum2)
Line Input #fnum2, sL
Print #fnum1, sL
Loop
Print #fnum1,
Close #fnum2
End If
FileName = Dir()
Loop
Close #fnum1
End Sub
方法二:
如果这些文本文件都是ANSI编码,那么可以使用命令行的Copy语句。
代码确实能做到很简洁,但就没有灵活性了。其中,文件将按“文件名”排序。
Private Sub Command1_Click()
Dim FolderPath As String
FolderPath = "D:\123\"
Shell "cmd.exe /c Copy """ & FolderPath & "*.txt"" """ & FolderPath & "tmp.txt"""
End Sub
另外,这种方法可以用bat文件来实现。
txt文件名有规律吗?比如1.txt 、2.txt 、3.txt....如果是这样的话就好弄点。。。
如果没规律可以用通用控件返回txt文本的路径,然后用input把txt内容都读取出来存入指定的文本就可以了