写了几个函数,可以完成的所有的功能.下有代码 .并有例子.
'''读取文本文件内的指定行.请不要设置太多.影响速度
'''FilePath 文件路径
'''LineValue 可选参数 返回指定行数
Function ReadFileLine(FilePath As String, Optional LineValue As Integer = 10) As String
If Len(Dir(FilePath)) = 0 Then Exit Function
Dim t As String, i As Integer, f As Integer
f = FreeFile
Open FilePath For Input As #f
Do While Not EOF(1)
Line Input #f, t
i = i + 1
If i = LineValue Then ReadFileLine = t: Exit Do
Loop
Close #f
End Function
'''从集合中找出新的文件名,并保存.每个文件只会返回一次,如果想初始 .请删除程序目录内容的info.ini文件
'''fileColl 文件名的集合.只会找出记录里没有的.没有的会添加到记录,下次调用不会出现在返回集合内
Function FindNewFile(fileColl As Collection) As Collection
Static TextPath As Collection
Dim s As String, f As Integer, fp As String
fp = App.path & "/info.ini"
f = FreeFile
If TextPath Is Nothing Then
Set TextPath = New Collection
If Len(Dir(fp)) > 0 Then
Open fp For Input As #f
Do While Not EOF(1)
Line Input #f, s
TextPath.Add s
Loop
Close #f
End If
End If
Dim tem As New Collection, t, i, find As Boolean
f = FreeFile
Open fp For Append As #f
For Each t In fileColl
find = False
For Each i In TextPath
If i = t Then find = True: Exit For
Next
If find = False Then
TextPath.Add t: tem.Add t
Print #f, t
End If
Next
Close #f
Set FindNewFile = tem
End Function
'''得到目录内容的文件名列表集合
'''path 要得到文件集合的路径
'''TypeStr 可选参数,指定文件的后缀
Function GetFiles(path As String, Optional TypeStr As String = "*") As Collection
Dim fs As New Collection, s As String
s = Dir(path & "/*." & TypeStr)
If Len(s) > 0 Then
Do
fs.Add s
s = Dir
Loop While Len(s) > 0
End If
Set GetFiles = fs
End Function
追问还需要读取每个文件夹内的第10行数据呀!谢谢
追答上面写的有这个函数 ReadFileLine
给它路径 返回的就是...
追问路径写在哪呀,如路径在E:\DATA\ 该如何填写谢谢
追答Private Sub Timer1_Timer()
Dim path As String
path = "E:\DATA"
Dim t As Collection
Set t = FindNewFile(GetFiles(path, "txt"))
Dim a
For Each a In t
List1.AddItem ReadFileLine(path & "\" & a)
Next
End Sub