;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfBinarySortTextFile (strFilenameIn, strFilenameOut, intDirection, intKeyColumn, intKeySize) intFileSize = FileSize (strFilenameIn) If intFileSize == 0 Then Return @FALSE If intDirection != @ASCENDING Then If intDirection != @DESCENDING Then Return @FALSE ; Create filename for output file if parameter strFilenameOut is empty. If strFilenameOut == "" Then strFilenameOut = ItemReplace (StrCat ("sorted.", ItemExtract (-1, strFilenameIn, ".")), -1, strFilenameIn, ".") ; Get maximum line size for sort record. intLineSize = 0 intLineCount = 0 IntControl (65, 4096 * 256, 0, 0, 0) ; Enlarge FileRead buffer for speedy access. ; Added DD.20030128 hdlFR = FileOpen (strFilenameIn, "READ") While @TRUE strLine = FileRead (hdlFR) If strLine == "*EOF*" Then Break intLineSize = Max (intLineSize, StrLen (strLine)) intLineCount = intLineCount + 1 EndWhile hdlFR = FileClose (hdlFR) ; Fill the buffer. intLineSize = intLineSize + 2 ; Include trailing @CRLF. hdlBB = BinaryAlloc (intLineCount * intLineSize) IntControl (65, 4096 * 256, 0, 0, 0) ; Enlarge FileRead buffer for speedy access. ; Added DD.20030128 hdlFR = FileOpen (strFilenameIn, "READ") intOffset = 0 While @TRUE strLine = FileRead (hdlFR) If strLine == "*EOF*" Then Break BinaryPokeStr (hdlBB, intOffset, strLine) intOffset = intOffset + intLineSize BinaryPokeStr (hdlBB, intOffset - 2, @CRLF) EndWhile hdlFR = FileClose (hdlFR) ; Sort the buffer. intKeyColumn = Max (1, intKeyColumn) iKeyOffset = intKeyColumn - 1 intKeySize = Max (0, intKeySize) If intKeySize == 0 Then intKeySize = intLineSize - iKeyOffset BinarySort (hdlBB, intLineSize, iKeyOffset, intKeySize, @STRING|intDirection) ; Delete the binary zeroes. BinaryReplace (hdlBB, "", "", @FALSE) ; Write the buffer to diskfile. BinaryWrite (hdlBB, strFilenameOut) hdlBB = BinaryFree (hdlBB) Return !!FileExist (strFilenameOut) ;.......................................................................................................................................... ; This UDF "udfBinarySortTextFile" sorts an input textfile ; in ascending or descending order by using WinBatch Binary Functions. ; If output filename is omitted then the function uses the input filename ; to create a similar output filename but with an additional suffix ".sorted". ; ; Parameter: ; strFilenameIn .............. The input textfile. ; strFilenameOut ............. The output textfile. ; intDirection=@ASCENDING .... Sort order alphabetic ascending. ; intDirection=@DESCENDING ... Sort order alphabetic descending. ; intKeyColumn ............... Start column of the sortkey, one based (first char=first column). ; intKeySize ................. Length of the sortkey. ; ; Return value ............... @TRUE (1) if all was ok resp. @FALSE (0) if something was wrong. ;.......................................................................................................................................... ; Detlev Dalitz.20010709.20020708.20030128.20090424.20100805. ;.......................................................................................................................................... #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfBrowseText (strFilename, strWindowTitle) ; Use WinBatch Browser to look into the text files. If Run (StrCat (DirHome (), "browser.exe"), strFilename) WinTitle (WinGetactive (), strWindowTitle) SendKeysTo (strWindowTitle, "^t") TimeDelay (1) EndIf #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strFilenameIn = FileCreateTemp ("IN") strFilenameOut = FileCreateTemp ("OUT") hdlFW = FileOpen (strFilenameIn, "WRITE") FileWrite (hdlFW, "FILEA UNIQUE") FileWrite (hdlFW, " COMMON") FileWrite (hdlFW, " File A DUP") FileWrite (hdlFW, " COMMON DUP") FileWrite (hdlFW, " File A DUP") FileWrite (hdlFW, " COMMON DUP") FileWrite (hdlFW, " File A 1") FileWrite (hdlFW, " File X 1") FileWrite (hdlFW, " File A 2") FileWrite (hdlFW, " File X 2") FileWrite (hdlFW, " File A 2") FileWrite (hdlFW, " File X 2") FileWrite (hdlFW, " File A 3") FileWrite (hdlFW, " File X 3") FileWrite (hdlFW, " File A 3") FileWrite (hdlFW, " File X 3") FileWrite (hdlFW, " File A 3") FileWrite (hdlFW, " File X 3") hdlFW = FileClose (hdlFW) udfBrowseText (strFilenameIn, "Input") While @TRUE intTestCase = AskItemlist ("Choose a testcase", "1,2,3,4", ",", @UNSORTED, @SINGLE) If intTestCase == "" Then Goto CANCEL Goto Test%intTestCase% :Test1 blnResult = udfBinarySortTextFile (strFilenameIn, strFilenameOut, @ASCENDING, 0, 0) udfBrowseText (strFilenameOut, "Sorted 1: Ascending, KeyColumn=0, KeyLength=0") Goto cleaning :Test2 blnResult = udfBinarySortTextFile (strFilenameIn, strFilenameOut, @DESCENDING, 0, 0) udfBrowseText (strFilenameOut, "Sorted 2: Descending, KeyColumn=0, KeyLength=0") Goto cleaning :Test3 ; Sort lines on column 10. blnResult = udfBinarySortTextFile (strFilenameIn, strFilenameOut, @ASCENDING, 10, 1) udfBrowseText (strFilenameOut, "Sorted 3: Ascending, KeyColumn=10, KeyLength=1") Goto cleaning :Test4 ; Sort lines on column 8. blnResult = udfBinarySortTextFile (strFilenameIn, strFilenameOut, @ASCENDING, 8, 3) udfBrowseText (strFilenameOut, "Sorted 4: Ascending, KeyColumn=8, KeyLength=3") Goto cleaning :Cleaning FileDelete (strFilenameOut) EndWhile :CANCEL FileDelete (strFilenameIn) FileDelete (strFilenameOut) Exit ;------------------------------------------------------------------------------------------------------------------------------------------