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.

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.

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=BinaryIndexEx(BufTarg,0,CtrlZ,@FWDSCAN,1)       ;Search for CtrlZ
   If x != -1                                     
       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
File Created: 2004:06:15:11:05:30
Last Updated: 2004:06:15:11:05:30