Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: binary search match
;;*************************************************************************** ;;** File: BinarySearch.wbt ;;** Requires: binary_test_filet.txt ;;** Author: Craig Storey ;;** Modified: 2001/10/02 ;;*************************************************************************** ;UDF: BLineSearch(inputfile, offset, clue, CS, moreinfo, failure_notice) ; ; inputfile - Full path + filename + extension of file to search ; offset - Binary offset location to start search ; clue - String to search for ; CS - Case-sensitivity (0=No, 1=Yes) ; moreinfo - BLineSearch Return (value) depends on the switch "moreinfo" ; - moreinfo = 0 then Return line or "failure_notice" ; - moreinfo = 1 then Return line or "failure_notice" + | buffer size | offset of clue | line begin | line end | line length ; - moreinfo = 2 then Return line or "failure_notice" + @CRLF buffer size @CRLF offset of clue @CRLF line begin @CRLF line end @CRLF line length ; failure_notice - String to return if nothing was found #DefineFunction BLineSearch(inputfile, offset, clue, CS, moreinfo, failure_notice) ;; Binary Search for a "CLUE" fs=FileSize(inputfile) binbuf = BinaryAlloc(fs) BinaryRead(binbuf, inputfile) index = BinaryIndexEx(binbuf, offset, clue, @FWDSCAN, CS) ;; If Clue was found return entire line If (index>=0) linebegin = BinaryIndex(binbuf, index, @CRLF, @BACKSCAN) + 2 ;; +2 for CRLF If (linebegin==2) Then linebegin=0 ;; Compensate for 1st line having no previous CRLF lineend = BinaryIndex(binbuf, linebegin, @CRLF, @FWDSCAN) linelen = lineend-linebegin linedata = BinaryPeekStr(binbuf, linebegin, linelen) ;; Included Extra info about the location, filesize, linelenght, etc.. Switch moreinfo Case 1 linedata=StrCat(linedata,"|",fs,"|", index,"|",linebegin,"|",lineend,"|",linelen) Break Case 2 linedata=StrCat(linedata,@CRLF,@CRLF,"File Size= ", fs, @CRLF, "Offset= ",index,@CRLF,"Line BEGIN= ",linebegin,@CRLF,"Line END= ",lineend,@CRLF,"Line LENGTH= ",linelen) Break Case DEFAULT linedata=linedata ;; No formatting Break EndSwitch Else linedata=failure_notice EndIf binbuf=BinaryFree(binbuf) Return(linedata) #EndFunction ;;*************************************************************************** ;; A simple example of how to use BLineSearch ;; requires the file below in the same directory ;;*************************************************************************** ;;--------------------------------------------------------------------------- ;; binary_test_filet.txt ;;--------------------------------------------------------------------------- ;; This is a binary_test_filet.txt ;; Here's the cue to start...Not quite right! ;; It was Colonel Mustart with the wrench in the study. ;; That's a pretty good clue! Because this sentence was found. ;; That's all folks. ;;--------------------------------------------------------------------------- IntControl(1002,0,0,0,0) ;;Hide WIL Box home=FilePath(IntControl(1004,0,0,0,0)) ;;Get Path to this File inputfile=StrCat(home, "binary_test_filet.txt") ;; File to search clue="clue" ;; What to look for? CS=0 ;; Case-sensitivity (0=No, 1=Yes) mess="%@CRLF%" ;; Results string filelength=100 ;; Arbitrary to begin with offset=0 ;; Start of Binary buffer moreinfo=1 ;; Return only string, not filesize etc.. failure_notice=-1 ;; Could also be "No Matches" While offset<filelength foundline=BLineSearch(inputfile, offset, clue, CS, moreinfo, failure_notice) ;; Message(1, foundline) If (foundline!=failure_notice) occurances=ItemCount(foundline, "|") ;; Count | incase there are extras. BLlength=ItemExtract(occurances, foundline, "|") ;; Work backwards to extract info BLend=ItemExtract(occurances-1, foundline, "|") BLbegin=ItemExtract(occurances-2, foundline, "|") offset=BLend ;; Move to the next line filelength=ItemExtract(occurances-4, foundline, "|") foundline=ItemExtract(1, foundline, "|") ;; Only the line found mess=StrCat(mess, foundline, @CRLF) ;; Add to other found lines. ;; Message(2, StrCat(filelength,":",offset)) Else offset=filelength+500 EndIf EndWhile Message(StrCat("BLineSearch for '",clue,"' Results"), mess) Exit
Article ID: W15002
File Created: 2008:11:24:15:19:40
Last Updated: 2008:11:24:15:19:40