VB如何按日期读取文本文件内的字符串

如题,文件夹内有N个文本文件,且不定时的在生成新的文本文件,如何用VB读取当前最新建立的文本文件内的第10行字符串!
在线等

写了几个函数,可以完成的所有的功能.下有代码 .并有例子.

'''读取文本文件内的指定行.请不要设置太多.影响速度
'''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

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-14
dim fo as object , f as date
Set fo = CreateObject
("Scripting.FileSystemObject")
f= fo.GetFile("c:\boot.ini")
这个方法可以获取到文件的创建日期,其他的我无能为力了。追问

还需要读取每个文件夹内的第10行数据呀!谢谢

还需要读取每个文件夹内的第10行数据呀!谢谢

第2个回答  2013-07-13
文本文件名的命名规律呢?追问

有规律下面是文本文件名称
5201306281636491
5 2013 06 28 16 36 491
意思是2013年06月28日16时36分49秒

相似回答