Open files in a loop
Posted: Tue Aug 29, 2023 9:58 am
I'm testing searching for text in files in a cache. I'm getting filenames, but the ReadString() function keeps returning empty.
Code: Select all
Procedure ReadCallback3(Status, Filename$, File, Size)
Protected Format, SearchText3$, CountFound
If Status = #PB_Status_Loaded
; Debug "|" + Filename$ + "|"
; Debug Size
; Debug "File: " + Filename$ + " - Size: " + Size + " bytes"
; Format = ReadStringFormat(g_id_file3)
Format = #PB_UTF8
; Debug g_id_file3
SearchText3$ = ReadString(g_id_file3, Format | #PB_File_IgnoreEOL)
Debug Filename$
CloseFile(g_id_file3)
; Debug SearchText3$
; Debug Len(SearchText3$)
If Asc(SearchText3$)
If MatchRegularExpression(g_re, SearchText3$)
Debug Filename$
EndIf
EndIf
ElseIf Status = #PB_Status_Error
Debug Filename$
EndIf
EndProcedure
Procedure btnAll()
Protected CountFound, i, id_file
SearchAll$ = GetGadgetText(#StrSearchAll)
If Len(SearchAll$) > 1 And g_CountF
g_re = CreateRegularExpression(0, SearchAll$, #PB_RegularExpression_AnyNewLine | #PB_RegularExpression_NoCase | #PB_RegularExpression_MultiLine)
If g_re
For i = 0 To g_CountF - 1
g_id_file3 = ReadFile(#PB_Any, "./data/" + aStrF$(i) + ".htm", @ReadCallback3())
; Debug aStrF$(i)
Next
FreeRegularExpression(g_re)
EndIf
EndIf
EndProcedure