| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1171 人关注过本帖, 1 人收藏
标题:VB单文件txt文件数据提取处理,如何改为批量txt文件数据提取处理呢
只看楼主 加入收藏
szchen2018
Rank: 2
等 级:论坛游民
帖 子:39
专家分:21
注 册:2021-11-20
结帖率:91.67%
收藏(1)
已结贴  问题点数:20 回复次数:4 
VB单文件txt文件数据提取处理,如何改为批量txt文件数据提取处理呢
以下VB代码实现的是提取当前目录下的Table.txt数据,然后创建Format.txt文件,按特定格式提取数据保存到里面。
想要改造为自动扫描当前目录下的.txt文件,然后按扫描到的txt文件名创建_Format.txt后缀,然后按特定格式提取数据保存到里面。
比如:当前目录下有123Table.txt,adadf.txt,iiiiii.txt,运行exe后按特定格式提取数据,然后保存为123123Table_Format.txt,adadf_Format.txt,iiiiii_Format.txt。

Private Sub Click()
    FileName = App.Path & "\" & "Table.txt"
    If Dir(FileName) <> "" Then
        Open FileName For Binary As #1
        AllData$ = Input$(LOF(1), 1)
        Close #1
        arr = Split(AllData$, vbCrLf)
        FileNameNew = Replace(LCase(FileName), ".txt", "Format.txt")
        Dim sr1 As String
        Dim sr2 As String
        Dim sr3 As String
        Open FileNameNew For Output As #1
        For X = 0 To UBound(arr)
            If InStr(arr(X), "Register_Table") > 0 Then
                Print #1, "//" & arr(X)
                Print #1, "const REG_TAB m_reg_tab[]={"
                k = 0
            ElseIf Left(arr(X), 3) = "Reg" Then
                k = k + 1
                sr1 = Mid(arr(X), 5, 2)
                sr2 = Mid(arr(X), 10, 2)
                sr3 = Mid(arr(X), 14, 99)
                Print #1, "    {0x" & sr1 & ",0x" & sr2 & "},//" & sr3
            ElseIf InStr(arr(X), "Ram_Table") > 0 Then
                k = 0
                Print #1, "//" & arr(X)
                Print #1, "const RAM1_TAB m_ram1_tab[]={"
            ElseIf Left(arr(X), 4) = "Coef" Then
                k = k + 1
                sr1 = Mid(arr(X), 6, 2)
                sr2 = Mid(arr(X), 11, 6)
                sr3 = Mid(arr(X), 18, 99)
                Print #1, "    {0x" & sr1 & ",0x" & sr2 & "},//" & sr3
            ElseIf arr(X) = "" And k > 0 Then
                k = 0
                Print #1, "};"
            End If
        Next
        Close #1
    End If
End Sub
搜索更多相关主题的帖子: Print txt 提取 数据 文件 
2021-11-20 16:22
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:20 
    Dim Filename As String, TmpFN As String
    TmpFN = App.Path & "\" & "*.txt"            '所有的TXT文件
    Filename = Dir(TmpFN)                       '返回第一个
   
    Do While Filename <> ""                     '文件名不为空则循环
        '注意,DIR返回值没有路径
        Filename = App.Path & "\" & Filename    '组合文件路径及文件名
            
            '接你原来的处理过程
            'Filename 已经组合好了
            
        
        Filename = Dir()                        '下一个文件名
    Loop

授人于鱼,不如授人于渔
早已停用QQ了
2021-11-20 16:37
szchen2018
Rank: 2
等 级:论坛游民
帖 子:39
专家分:21
注 册:2021-11-20
收藏
得分:0 
回复 2楼 风吹过b
感谢回答,刚出门了,晚点回去我试试
2021-11-20 16:44
szchen2018
Rank: 2
等 级:论坛游民
帖 子:39
专家分:21
注 册:2021-11-20
收藏
得分:0 
回复 2楼 风吹过b
您好,我试了编译会报Loop缺少Do,看前面已经是有Do While的了,麻烦帮忙看看是哪里出问题了呢,谢谢!
Private Sub Click()
    Dim Filename As String, TmpFN As String
    TmpFN = App.Path & "\" & "*.txt"            '所有的TXT文件
    Filename = Dir(TmpFN)                       '返回第一个
    Do While Filename <> ""                     '文件名不为空则循环
        '注意,DIR返回值没有路径
        Filename = App.Path & "\" & Filename    '组合文件路径及文件名
            '接你原来的处理过程
            'Filename 已经组合好了
        Open Filename For Binary As #1
        AllData$ = Input$(LOF(1), 1)
        Close #1
        arr = Split(AllData$, vbCrLf)
        FileNameNew = Replace(LCase(Filename), ".txt", "Format.txt")
        Dim sr1 As String
        Dim sr2 As String
        Dim sr3 As String
        Open FileNameNew For Output As #1
        For X = 0 To UBound(arr)
            If InStr(arr(X), "Register_Table") > 0 Then
                Print #1, "//" & arr(X)
                Print #1, "const REG_TAB m_reg_tab[]={"
                k = 0
            ElseIf Left(arr(X), 3) = "Reg" Then
                k = k + 1
                sr1 = Mid(arr(X), 5, 2)
                sr2 = Mid(arr(X), 10, 2)
                sr3 = Mid(arr(X), 14, 99)
                Print #1, "    {0x" & sr1 & ",0x" & sr2 & "},//" & sr3
            ElseIf InStr(arr(X), "Ram_Table") > 0 Then
                k = 0
                Print #1, "//" & arr(X)
                Print #1, "const RAM1_TAB m_ram1_tab[]={"
            ElseIf Left(arr(X), 4) = "Coef" Then
                k = k + 1
                sr1 = Mid(arr(X), 6, 2)
                sr2 = Mid(arr(X), 11, 6)
                sr3 = Mid(arr(X), 18, 99)
                Print #1, "    {0x" & sr1 & ",0x" & sr2 & "},//" & sr3
            ElseIf arr(X) = "" And k > 0 Then
                k = 0
                Print #1, "};"
            End If
        Filename = Dir()                        '下一个文件名
    Loop
End Sub
2021-11-20 21:21
szchen2018
Rank: 2
等 级:论坛游民
帖 子:39
专家分:21
注 册:2021-11-20
收藏
得分:0 
报错解决了,最后我漏了Next Close #1,验证完全符合需求,非常感谢大佬
        Next
        Close #1
        Filename = Dir()                        '下一个文件名
    Loop
2021-11-20 21:37
快速回复:VB单文件txt文件数据提取处理,如何改为批量txt文件数据提取处理呢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041599 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved