VB读取TXT文件并获取指定内容

ajunajun 2014-02-08 09:23:52
文件格式如下:
2007/7/9 0:00:00 24.899
2007/7/9 1:00:00 24.887
2007/7/9 2:00:00 24.878
2007/7/9 3:00:00 24.862
2007/7/9 4:00:00 24.856
2007/7/9 5:00:00 24.85
2007/7/9 6:00:00 24.844
2007/7/9 7:00:00 24.881
2007/7/9 8:00:00 24.916
2007/7/9 9:00:00 24.93
2007/7/9 10:00:00 24.945
2007/7/9 11:00:00 24.956
2007/7/9 12:00:00 24.966
2007/7/9 13:00:00 24.97
2007/7/9 14:00:00 24.978
2007/7/9 15:00:00 24.974
2007/7/9 16:00:00 24.982
2007/7/9 17:00:00 24.977
2007/7/9 18:00:00 24.979
2007/7/9 19:00:00 24.935
2007/7/9 20:00:00 24.9
2007/7/9 21:00:00 24.876
2007/7/9 22:00:00 24.858
2007/7/9 23:00:00 24.839
2007/7/10 0:00:00 24.826
2007/7/10 1:00:00 24.813
2007/7/10 2:00:00 24.805
2007/7/10 3:00:00 24.791
2007/7/10 4:00:00 24.787
2007/7/10 5:00:00 24.775
2007/7/10 6:00:00 24.772
2007/7/10 7:00:00 24.802
2007/7/10 8:00:00 24.851
2007/7/10 9:00:00 24.873
2007/7/10 10:00:00 24.838
2007/7/10 11:00:00 24.858
2007/7/10 12:00:00 24.869
2007/7/10 13:00:00 24.878
2007/7/10 14:00:00 24.879
2007/7/10 15:00:00 24.883
2007/7/10 16:00:00 24.887
2007/7/10 17:00:00 24.894
2007/7/10 18:00:00 24.897
2007/7/10 19:00:00 24.857
2007/7/10 20:00:00 24.829
2007/7/10 21:00:00 24.806
2007/7/10 22:00:00 24.787
2007/7/10 23:00:00 24.769
……
怎样顺序读取文件,提取每天8点的那一行(红色字体),并生成新文件,
输出文件格式如下:
2007070908 24.916
2007071008 24.851
……

...全文
559 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
of123 2014-02-10
  • 打赏
  • 举报
回复
哦?需求变了?
Private Sub Command1_Click()
Dim strLine As String, strItems() As String

    Open "c:\test\1.txt" For Input As #1
    Open "c:\test\2.txt" For Output As #2
    Do Until EOF(1)
        Line Input #1, strLine
        strItems = Split(Trim(strLine), " ")
        'If UBound(strItems) = 2 Then
            If strItems(1) = "8:00:00" Then
                strLine = Format(strItems(0), "yyyymmdd") & Format(strItems(1), "HH") & " " & strItems(UBound(strItems))
                Print #2, strLine
            End If
        'End If
    Loop
    Close #2
    Close #1

End Sub
ajunajun 2014-02-08
  • 打赏
  • 举报
回复
引用 3 楼 ajunajun 的回复:
怎么输出文件没内容啊?



每行的空格数都不同。
ajunajun 2014-02-08
  • 打赏
  • 举报
回复
怎么输出文件没内容啊?
of123 2014-02-08
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim strLine As String, strItems() As String

    Open "c:\test\1.txt" For Input As #1
    Open "c:\test\2.txt" For Output As #2
    Do Until EOF(1)
        Line Input #1, strLine
        strItems = Split(Trim(strLine), " ")
        If UBound(strItems) = 2 Then
            If strItems(1) = "8:00:00" Then
                strLine = Format(strItems(0), "yyyymmdd") & Format(strItems(1), "HH") & " " & strItems(2)
                Print #2, strLine
            End If
        End If
    Loop
    Close #2
    Close #1
End Sub
2007070908 24.916
2007071008 24.851
of123 2014-02-08
  • 打赏
  • 举报
回复
很多方法,最简单的是逐行读取,进行比对。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧