首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FSO中的ReadAll()方法没有完成文本文件

FSO中的ReadAll()方法没有完成文本文件
EN

Stack Overflow用户
提问于 2022-07-19 16:45:41
回答 1查看 105关注 0票数 0

我正在尝试一次将一个文本文件读入字符串中,以搜索我所知道的存在于其中的值。我使用的是FSO方法(ReadAll()),但是Instr()函数找不到这个值。我认为他只是进口了文件的一部分。

代码语言:javascript
复制
Sub read_File_At_once_FSO()

On Error Resume Next
Dim objFileSystemObject As Object
Dim strFileContent As String
Dim RowsInFile  As Long
'Dim ColsInFile  As Long
Dim FileSize    As Long 'in bytes
Dim InStrPos    As Long
Dim strFullPath As String


    strFullPath = "G:\Fusao de Clientes\PRD\IMP_ID1056001_J.TXT"
    
    ' Use late binding throughout this method to avoid having to set any references.
    Set objFileSystemObject = CreateObject("Scripting.FileSystemObject")
    strFileContent = objFileSystemObject.OpenTextFile(strFullPath).readall()
    
    
        
                ' We have a matched string.
                InStrPos = InStr(1, strFileContent, "0005790306", vbTextCompare)
    
                If InStrPos > 0 Then
                    
                    'get FileLen
                    FileSize = FileLen(strFullPath)
                    
                    'Get the number of lines inside the file
                    With CreateObject("vbscript.regexp")
                        .Global = True
                        '.Pattern = "\b" & varStrings(lngIndex) & "\b"     'By using word boundary (\b), you can specify in the regex pattern that you are searching for complete word(s).        '"\r\n"       '  or .Pattern = "\n"
                        .Pattern = "\r\n" 'vbCrLf               '.Pattern = "\n" ' vbLf, Unix style line-endings
                        RowsInFile = .Execute(strFileContent).Count + 1
                    End With
                    
                    
                    
                    
                End If
            
       
        Set objFileSystemObject = Nothing
On Error GoTo 0


End Sub

我不知道如何让你对这个文件进行测试。

当我试图阅读下面的代码时,我得到的是“运行时错误62 :输入文件的结束”。

代码语言:javascript
复制
Function read_File_At_once()

Dim strFilename As String: strFilename = "G:\Fusao de Clientes\PRD\IMP_ID1056001_J.TXT"
Dim strFileContent As String
Dim iFile As Integer: iFile = FreeFile
Open strFilename For Input As #iFile
strFileContent = Input(LOF(iFile), iFile)

    'Get the number of lines inside the file
    With CreateObject("vbscript.regexp")
        .Global = True
        '.Pattern = "\b" & varStrings(lngIndex) & "\b"     'By using word boundary (\b), you can specify in the regex pattern that you are searching for complete word(s).        '"\r\n"       '  or .Pattern = "\n"
        .Pattern = "\r\n" 'vbCrLf               '.Pattern = "\n" ' vbLf, Unix style line-endings
        RowsInFile = .Execute(strFileContent).Count + 1
    End With


Close #iFile
End Function

有人能帮忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-21 16:14:49

你的文件是UTF-8,FSO不擅长阅读.您可以使用下面这样的函数,它将正确读取整个文件:

代码语言:javascript
复制
Function ReadUTF8(filePath As String) As String
    With CreateObject("ADODB.Stream")
        .Charset = "utf-8"
        .Open
        .LoadFromFile filePath
        ReadUTF8 = .ReadText()
        .Close
    End With
End Function

编辑-显然FSO Read()方法可以代替ReadAll()

代码语言:javascript
复制
Function FsoReadAll(filePath As String) As String
    With CreateObject("Scripting.FileSystemObject")
        FsoReadAll = .OpenTextFile(filePath, 1).read(.getFile(filePath).Size)
    End With
End Function

来自:https://stackoverflow.com/a/56434461/478884

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73040715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档