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.

Sample Code to Sort a Flat File

Keywords: 	 sort a flat file

;Make a test file to sort
; 4 fields
;Field 1  chars 1-20   len 20
;space
;Field 2  chars 22-25  len 4
;space
;Field 3  chars 27-30  len 4
;space
;Field 4  char  32-78  len 47
;Dummy field 5  79-80 @CRLF

;for this test case fields 1 2 and 4 are identical.  Field 3 is a random
;number we will sort on later.

;Create test file
fn="c:\incoming\test.dat"
fnsort="c:\incoming\test.sort"
handle=FileOpen(fn,"WRITE")
f1=strfix("f1f1f1f1"," ",20)
f2=strfixleft(555,0,4)
f4=strfix("random text"," ",47)

for xx=1 to 200
   f3=strfixleft(Random(9999),0,4)
   FileWrite(handle,strcat(f1," ",f2," ",f3," ",f4))
next

FileClose(handle)

;Now sort it of field 3

fs=FileSize(fn)
bb=BinaryAlloc(fs)
BinaryRead(bb,fn)
recsize=80
keyoffset=26  ; Field 3 offset  0 based
keysize=4

BinarySort(bb,recsize,keyoffset,keysize,@ASCENDING)  ; string sort

BinaryWrite(bb,fnsort)
BinaryFree(bb)
Message("ALL","DONED")





Article ID:   W14848
File Created: 2001:11:08:12:40:24
Last Updated: 2001:11:08:12:40:24