I'm using VBA in order to read through a file byte by byte.
Ultimately I want to remove some invalid characters from a textfile.
This code works fine, but it is very slow for files exceeding 100MB.
Is there a way to make this work faster? I suspect that one should read blocks of multiple bytes, but then what would the code look like?
Ultimately I want to remove some invalid characters from a textfile.
Code:
[SIZE=2]Option Compare Database[/SIZE]
[SIZE=2]Option Explicit[/SIZE]
[SIZE=2] [/SIZE]
[SIZE=2]Sub TestFunction()[/SIZE]
[SIZE=2]MsgBox "fn_ReadWriteStream started"[/SIZE]
[SIZE=2]If fn_ReadWriteStream("C:\Project\Utilities\Access en VB\ReadWriteStream\Macro_mcr_all_qry.txt") = True Then[/SIZE]
[SIZE=2]MsgBox "fn_ReadWriteStream succeeded"[/SIZE]
[SIZE=2]Else[/SIZE]
[SIZE=2]MsgBox "fn_ReadWriteStream failed"[/SIZE]
[SIZE=2]End If[/SIZE]
[SIZE=2]End Sub[/SIZE]
[SIZE=2]Public Function fn_ReadWriteStream(pFileName As String) As Boolean[/SIZE]
[SIZE=2]Dim fname As String[/SIZE]
[SIZE=2]Dim fname2 As String[/SIZE]
[SIZE=2]Dim fnr As Integer[/SIZE]
[SIZE=2]Dim fnr2 As Integer[/SIZE]
[SIZE=2]Dim tstring As String * 1[/SIZE]
[SIZE=2]Dim i As Integer[/SIZE]
[SIZE=2]fn_ReadWriteStream = False[/SIZE]
[SIZE=2]fname = pFileName[/SIZE]
[SIZE=2]fname2 = pFileName & ".clean.txt"[/SIZE]
[SIZE=2]fnr2 = FreeFile()[/SIZE]
[SIZE=2]Open fname2 For Binary Lock Read Write As #fnr2[/SIZE]
[SIZE=2]fnr = FreeFile()[/SIZE]
[SIZE=2]Open fname For Binary Access Read As #fnr[/SIZE]
[SIZE=2]Do[/SIZE]
[SIZE=2]Get #fnr, , tstring[/SIZE]
[SIZE=2]If EOF(fnr) Then Exit Do[/SIZE]
[SIZE=2]If Asc(tstring) = 254 Or _[/SIZE]
[SIZE=2]Asc(tstring) = 255 Or _[/SIZE]
[SIZE=2]Asc(tstring) = 0 Then[/SIZE]
[SIZE=2]Else[/SIZE]
[SIZE=2]Put #fnr2, , tstring[/SIZE]
[SIZE=2]End If[/SIZE]
[SIZE=2]Loop[/SIZE]
[SIZE=2]Close #fnr[/SIZE]
[SIZE=2]Close #fnr2[/SIZE]
[SIZE=2]fn_ReadWriteStream = True[/SIZE]
[SIZE=2]End Function[/SIZE]
Is there a way to make this work faster? I suspect that one should read blocks of multiple bytes, but then what would the code look like?