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

File Operations

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

How to Search for Special Characters in a File

Keywords:     upper ascii control characters fileread  BinaryPoke

Question:

How does one search a file for the upper ascii characters (box drawing characters)?

Answer:

It depends.

The easy way - and this will work for a lot of files - is simply read it in line by line, searching each line with the StrScan function. You do need to set some variable up containing all the box characters.

Another way is to load the whole file into a binary buffer and hit it with a whole bunch of BinaryIndex's - one for each box character.

Question (Related Thread):

I need help solving a little puzzle ...

I need to open a file, do a search & replace function, and close the file. Sounds simple enough.

The catch is, I need to search for every instance of a CRLF, and replace with &HFF, &H04 and &H00.

One of my customers has a DOS application that requires &HFF &H04 &H00 instead of carriage returns. I have written a WinBatch application that has to keep a log that is read by the old DOS stuff. When I do what needs to be done, I write out a one-line message, and tack it on to the end of the existing logfile. Unfortunately, it corrupts when they read it since the DOS app can't handle a standard CRLF!

Any ideas? I've been looking at the Binary functions in WinBatch, but can't seem to come up with a winning combination.

Answer:

It's gonna be something like this:
; these two variables are set up by the user
logfile="C:\logs\logfile.txt"
linetoadd="Logged line to add to end of logfile"

; here we go (note:  code not debugged)

logsize=FileSize(logfile)+strlen(linetoadd)+100    ; adding a little extra...

bb=BinaryAlloc(logsize)                            ;Allocate binary buffer
BinaryRead(bb,logfile)                             ;Read in existing logfile
BinaryPokeStr(bb,BinaryEODGet(bb),line2add)        ;Throw line on end
BinaryPoke(bb,BinaryEODGet(bb),255)                ;followed by &HFF
BinaryPoke(bb,BinaryEODGet(bb),4)                  ;followed by &H04
BinaryPoke(bb,BinaryEODGet(bb),00)                 ;followed by &H00
BinaryWrite(bb,logfile)                            ;Write data back to file
BinaryFree(bb)                                     ; All done now

Article ID:   W13245
Filename:   Search for Upper Ascii Chars in File.txt
File Created: 1999:04:15:16:52:44
Last Updated: 1999:04:15:16:52:44