Wilson WindowWare Tech Support

WinBatch WinBatch+Compiler WebBatch
Home | Tech Database | Tech BBS | White Papers | Purchase


Using FileAppend for ASCII files with Ctrl-Z's

Keywords: FileAppend ASCII Ctrl Z

The FileAppend function glues files together assuming they are binary files. Some files, mostly legacy files from older DOS programs, use a Ctrl-Z ( ^Z ) character to mark the end of data. This code shows how to glue two files onto a third when it is understood that the end of data in any of them may be indicated by a Ctrl-Z

The trick involves the shortening of the defined "good data" in the target buffer when a Ctrl-Z exists in it. There is a slight complication if the first character in the target buffer is a Ctrl-Z, meaning the file is logically empty. The FIXTARGET code below handles the situation.

   
;FIXTARGET.WBT

Source1="xxx.txt"     ;Must exist
Source2="yyy.txt"     ;Must exist
Target="target.txt"   ;May optionally exist

CtrlZ=Num2Char(26)

Size1=FileSize(Source1)
Size2=FileSize(Source2)
SizeTarg=0
If FileExist(Target) then SizeTarg=FileSize(Target)
SizeTarg=SizeTarg+Size1+Size2       ; Target buffer must hold all the data

BufTarg=BinaryAlloc(SizeTarg)

if FileExist(target) then BinaryRead(BufTarg,Target)

gosub FixTarget
BufSrc1=BinaryAlloc(Size1)
BinaryRead(BufSrc1,Source1)
BinaryCopy(BufTarg,BinaryEODGet(BufTarg),BufSrc1,0,BinaryEODGet(BufSrc1))
BinaryFree(BufSrc1)


gosub FixTarget
BufSrc2=BinaryAlloc(Size2)
BinaryRead(BufSrc2,Source2)
BinaryCopy(BufTarg,BinaryEODGet(BufTarg),BufSrc2,0,BinaryEODGet(BufSrc2))
BinaryFree(BufSrc2)


BinaryWrite(BufTarg,Target)
BinaryFree(BufTarg)

exit


:FixTarget
if BinaryEODGet(BufTarg) > 0                     ;Any data in buffer at all??
   x=BinaryIndex(BufTarg,0,CtrlZ,@FWDSCAN)       ;Search for CtrlZ
   if x==0                                       ;Ambigious result.  Check
       y=BinaryPeekStr(BufTarg,0,1)              ;Grab First Character
       if y==CtrlZ then BinaryEODSet(BufTarg,0)  ;Zero data if it is a CtrlZ
   else
       BinaryEODSet(BufTarg,x)                   ;Shorten buffer, removing CTRLZ
   endif
endif
return





Article ID:   W13233
Filename:   File Append for ASCII files with Ctrl-Zs in them.txt