0

I'm trying to read a text file at once, into a string, to search for a value that I know that exists inside. I'm using the FSO Method (ReadAll()), but the Instr() function doesn't find that value. I think he is importing just a part of the file.

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

I dont know how to atach the file for you to test it.

When I try to read with the code below, I'm geting the 'Run Time Error 62 : Imput past end of file'.

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

Can anyone help please?

  • What's the encoding of the input file? UTF-8? UTF-16? You can't attach a file here, but you can share it from google drive/onedrive/etc – Tim Williams Jul 19 '22 at 16:51
  • what is the weight of the file? – Mik Jul 19 '22 at 17:17
  • @Tim Williams, how can I check if the file is UTF-8, OR UTF-16? –  Jul 19 '22 at 21:07
  • If you open it in a text editor it may tell you the encoding. Eg. using VS Code the encoding is shown in the bottom-right of the window. Or see for example - https://softwareengineering.stackexchange.com/questions/187169/how-to-detect-the-encoding-of-a-file – Tim Williams Jul 19 '22 at 21:51
  • Thank you @Tim Williams, tomorrow I'll check it in my office. –  Jul 19 '22 at 23:05
  • @Tim Williams, The file size is 622.026 bytes and has 282 columns and 2.215 rows. I've opened with Notepad++ and EditPlus, and both are setting encoding to ANSI. I''ll try to send you a lynk from google drive. Many thank's... –  Jul 20 '22 at 13:10
  • @Tim Williams, Here is the link for the file: https://1drv.ms/t/s!AjZfGWEA2CaVgW5d1RjaqfNv3NU8 –  Jul 21 '22 at 00:22
  • Hi dear @Tim Williams, Can you tell me please if you accessed the file, through the link, with success? –  Jul 21 '22 at 09:14
  • I've tested reading line by line, with ReadLine from fso.OpenTextFile, and the process reads the 2225 lines. Reading into a string, with ReadALL(), only reads 408 lines. –  Jul 21 '22 at 13:53

1 Answers1

0

Your file is UTF-8, which FSO is not good at reading. You can use something like the function below, and it will read the entire file correctly:

Function ReadUTF8(filePath As String) As String
    With CreateObject("ADODB.Stream")
        .Charset = "utf-8"
        .Open
        .LoadFromFile filePath
        ReadUTF8 = .ReadText()
        .Close
    End With
End Function

EDIT - apparently the FSO Read() method can work in place of ReadAll()

Function FsoReadAll(filePath As String) As String
    With CreateObject("Scripting.FileSystemObject")
        FsoReadAll = .OpenTextFile(filePath, 1).read(.getFile(filePath).Size)
    End With
End Function

From: https://stackoverflow.com/a/56434461/478884

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • may I ask how you came to that conclusion, the UTF-8? and, do this function read much bigger files? –  Jul 21 '22 at 22:37
  • I opened the file in VS Code, and it tagged it as UTF-8 I've never tested how large a file it could read. – Tim Williams Jul 21 '22 at 22:41
  • Thank you very much for all support. I'll prepare the process to read all files in folders, and search for strings inside. Thank's once more. –  Jul 22 '22 at 15:05