Open files in a loop

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Posts: 76
Joined: Wed Dec 14, 2022 1:13 pm

Open files in a loop

Post by AZJIO »

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
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Open files in a loop

Post by Peter »

g_id_file3 is unnecessary. Replace g_id_file3 with File in ReadCallback3()

Code: Select all

Format = ReadStringFormat(File)
[...]
SearchText3$ = ReadString(File, Format | #PB_File_IgnoreEOL)
[...]
CloseFile(File)
AZJIO
Posts: 76
Joined: Wed Dec 14, 2022 1:13 pm

Re: Open files in a loop

Post by AZJIO »

Thanks, it works.
Is there a way to check when the callback has finished processing files? For me, the loop ends quickly and the file menu drops out, but the callback continues to work and populates the menu, which automatically expands. I'd like to expand the menu when the callback is done, because if the menu doesn't fit the screen, then the file scrolling doesn't appear, and if I reopen the menu, it takes the number of items at once and adds scrolling. Also, if nothing is found, I would like to return the text "Nothing found", but I don't know when it will end and the algorithm considers that nothing was found instantly, although the search is not over yet.
Post Reply