SyntaxBoom

Languages & Coding => BlitzMax (NG/Vanilla variants) => Tutorials => Topic started by: Hardcoal on Jun 28, 2025, 01:10 PM

Title: Search Text In File
Post by: Hardcoal on Jun 28, 2025, 01:10 PM

Function LoadTextAndSearch(SearchFor:String, FileURL:String)
    SearchWantedTextInFile(SearchFor, LoadText(FileURL))
End Function

Function SearchWantedTextInFile(WantedText:String,FileText:String)
    WantedText = Lower(WantedText)
    Local LinesSplit:String[] = FileText.split("~n")
    Local Cntr
    For Local AL:String = EachIn LinesSplit
Cntr = Cntr + 1
Local LENT = Len(WantedText)
For Local I = 1 To Len (AL) - LENT
      If WantedText = Lower(Mid(AL, I, LENT)) Then Print   
            "WantedText Found At Line: " + Cntr
Next
    Next
End Function

Title: Re: Search Text In File
Post by: Hardcoal on Jun 28, 2025, 01:28 PM
10 minutes later... lol

Here is a recursive version of same thing..
This will Also Search Folders And Sub Folders


MyTextSreacher_Req("IgnoreLackOfExtention", "TestFolder")

Function MyTextSreacher_Req(SearchFor:String, FolderOrFilePath:String)

Select FileType (FolderOrFilePath)
     'Folder
Case 2
Local DirList:String[] = LoadDir(FolderOrFilePath)
For Local D:String = EachIn DirList
MyTextSreacher_Req(SearchFor, FolderOrFilePath + "/" + D)
Next
     'File
Default
SearchWantedTextInFile(SearchFor, FolderOrFilePath)
End Select

End Function

Function SearchWantedTextInFile(WantedText:String, FileURL:String)
Local FileText:String = LoadText(FileURL)
WantedText = Lower(WantedText)
Local LinesSplit:String[] = FileText.split("~n")
Local Cntr
For Local AL:String = EachIn LinesSplit
Cntr = Cntr + 1
Local LENT = Len(WantedText)
For Local I = 1 To Len (AL) - LENT
If WantedText = Lower(Mid(AL, I, LENT)) Then Print "WantedText Found At File: " + FileURL + " At Line: " + Cntr
Next
Next

End Function

Title: Re: Search Text In File
Post by: _PJ_ on Jul 11, 2025, 07:04 PM
That's a nifty use of

String.split("~n")

To break up the text into smaller pieces to work through.

I am curious if it is better to use String.Contains(WantedText) or Instr(String,Wantedtext,Cntr) for example when actual checking within those lines?
Maybe these commands do the samee iteration "under the hood)?
Title: Re: Search Text In File
Post by: Hardcoal on Jul 11, 2025, 08:11 PM
Honestly I wasnt even Aware of String.Contains..
But I think its case Sensitive, and I dont want that.

Sometimes (or many times) I code things that already exists.
And later I realize that.

You cant know everything
Title: Re: Search Text In File
Post by: Dabzy on Jul 12, 2025, 03:48 AM
I was wanting some PDF functions in Max-NG... Couldnt find any in the places I would suspect it should be in modules.

So I posted of any good libraries for it I could use instead, Derron pointed me to a few.

Further down the line, and though and behold, there was actually a mod for it, tucked out of the way, so I posted, and even Derron, who is pretty much "The BlitzMax-NG Guy" didnt actually know that either!

Just goes to show!

Dabz
Title: Re: Search Text In File
Post by: Hardcoal on Jul 12, 2025, 11:41 AM
Debzy, I always said youre a genius..
but nobody would listen
Title: Re: Search Text In File
Post by: Dabzy on Jul 12, 2025, 11:51 AM
Anar... Nobody does... They have me down as a gobshite as well (They'll not be wrong though on that front, cos I know I can talk a load of cack when I have the chance  :P)

;)

Dabz
Title: Re: Search Text In File
Post by: Baggey on Jul 12, 2025, 06:32 PM
Quote from: Dabzy on Jul 12, 2025, 03:48 AMI was wanting some PDF functions in Max-NG... Couldnt find any in the places I would suspect it should be in modules.

So I posted of any good libraries for it I could use instead, Derron pointed me to a few.

Further down the line, and though and behold, there was actually a mod for it, tucked out of the way, so I posted, and even Derron, who is pretty much "The BlitzMax-NG Guy" didnt actually know that either!

Just goes to show!

Dabz

I could write you some  ;)  8)
Title: Re: Search Text In File
Post by: Dabzy on Jul 12, 2025, 09:07 PM
Oh, this was a good few full moons back, the ones in the module worked perfectly fine me auld fruit! ;)

But just goes to show... Hidden in plain sight! :)

Dabz