WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

String Manipulation

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

bStringReplace UDF

Keywords:    bStringReplace udf

;;***************************************************************************
;;** File: BStringReplaceUDF.wbt
;;** Requires: binary_test_filet.txt
;;** Author: Craig Storey, cstorey@canada.com
;;** Modified: 2001/10/02
;;***************************************************************************


;UDF: BStringReplace(inputfile, offset, clue, replacement, CS, failure_notice)
;
; inputfile - Full path + filename + extension of file to search
; offset - Binary offset location to start search
; clue - String to search for
; replacement - String to replace "clue"
; CS - Case-sensitivity (0=No, 1=Yes)
; failure_notice - String to return if nothing was found
;
;*** WARNING!! ***;
; > Watch out for parentheses replacement! WinBatch has three working parentheses for 
; defining variables: ",',` make sure you don't enclose the parenthese to replace with
; itself. (Eg. Replace " with `. BAD replacement=""", GOOD replacement="`" or ='`')
;
; > Watch out for %, you need %% to either replace or insert a % sign in a file.
;*** WARNING!! ***;


#DefineFunction BStringReplace(inputfile, offset, clue, replacement, CS, failure_notice)

;; Binary Search for a "CLUE"
fs=FileSize(inputfile)
srcbuf = BinaryAlloc(fs) ;; Search buffer
buf2 = BinaryAlloc(fs+10000) ;; Top buffer
buf3 = BinaryAlloc(fs) ;; Bottom buffer
BinaryRead(srcbuf, inputfile)
index = BinaryIndexEx(srcbuf, offset, clue, @FWDSCAN, CS)

If (index>=0)  ; changed > to >= jun30/04 mw bugfix for location 0
linebegin = BinaryIndex(srcbuf, index, @CRLF, @BACKSCAN) + 2 ;; Find start of line with "Clue" + 2 for CRLF
If (linebegin==2) then linebegin=0 ;; Compensate for 1st line having no previous CRLF
lineend = BinaryIndex(srcbuf, linebegin, @CRLF, @FWDSCAN) ;; Find end of line with "Clue"
linelen = lineend-linebegin+1 ;; Length of "Clue" line in buffer
linedata = BinaryPeekStr(srcbuf, linebegin, linelen) ;; Grab string from buffer
linedata = StrReplace(linedata, clue, replacement) ;; Replace all "Clue" strings in that line
If (CS==0) 
linedata = StrReplace(linedata, STRUPPER(clue), replacement) ;; Replace all "Clue" strings in UPPER cases
linedata = StrReplace(linedata, strlower(clue), replacement) ;; Replace all "Clue" strings in lower cases
Endif
linedata=StrTrim(linedata)

BinaryCopy(buf2, 0, srcbuf, 0, linebegin) ;; Buffer upto "Clue" starts
BinaryCopy(buf3, 0, srcbuf, lineend, BinaryEodGet(srcbuf)-lineend) ;; Buffer from "Clue" end to EOF
BinaryPokeStr(buf2, linebegin, linedata) ;; Add new String to buf2
BinaryCopy(buf2, BinaryEodGet(buf2)-1, buf3, 0, BinaryEodGet(buf3)) ;; Append the two buffers

FileCopy(inputfile, StrCat(FilePath(inputfile), "!", FileRoot(inputfile), ".", FileExtension(inputfile)), @FALSE)
BinaryWrite(buf2, inputfile)
linedata=1

Else
linedata=failure_notice
Endif

;;Free all used memory
binbuf=BinaryFree(srcbuf)
binbuf2=BinaryFree(buf2)
binbuf3=BinaryFree(buf3)

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` Mustard with the wrench in the study.
;; That's a pretty good clue! Why? 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

;DebugTrace(@ON, StrCat(home, "BugReport.txt")) ;; Not much help with Binary Stuff

inputfile=StrCat(home, "binary_test_file.txt") ;; File to search
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"
foundline=""
count=-1

clue="e" ;; What to look for?
replacement="@#*" ;; Replacement for clue

While foundline!=failure_notice
foundline=BStringReplace(inputfile, offset, clue, replacement, CS, failure_notice)
count=count+1
Endwhile

If (count>=1)
AskFileText("BStringReplace Results", inputfile, @unsorted, @single)
Else
Message("BStringReplace Results", "Nothing to replace in file!")
Endif

Exit 



Article ID:   W15335
File Created: 2004:06:30:10:08:14
Last Updated: 2004:06:30:10:08:14