Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: binarysort binarypeekstr
The basic steps of my WinBatch are as follows:
It seems that the BinarySort Function is the problem since I can do all of the above EXCEPT for step 5 (the sort) and the output file of step 6 is identical to the file from step 4. It also doesn't matter if I just run the .WBT file or compile it.
I've Tried using STRFIX, STRFIXL., STRFIXCHARS and STRFIXCHARSL,; The FILE I'm creating is fixed length records when I check it with a text editor.
;================Here's a snippet of code===================== DLen = 50 If DirExist (strcat(NTBDsk,"\Temp")) == @FALSE Then DirMake (strcat(NTBDsk,"\Temp")) ;; Ask User for Date to Use Dt1 = AskLine("DAILY ACCOUNTING", "Enter Date (MMDDYYYY) to Process", "") MM = StrSub(Dt1, 1, 2) DD = StrSub(Dt1, 3, 2) YY = StrSub(Dt1, 5, 4) Date = StrCat(MM, "-", DD, "-", YY) ;; Build FileNames to Use Inf1 = StrCat(Drv,"\Logs\", Dt1, ".Log") Tmp1 = StrCat(NTBDsk,"\Temp\", Dt1, ".Wr1") Tmp2 = StrCat(NTBDsk,"\Temp\", Dt1, ".Wr2") Tmp3 = StrCat(NTBDsk,"\Temp\", Dt1, ".Wr3") Out1 = StrCat(Dt1, ".Dly") ;; Process the Input File FileCopy(Inf1, Tmp1, @FALSE) Inf1 = FileOpen(Tmp1, "READ") Out1 = FileOpen(Tmp2, "APPEND") While @TRUE InLn1 = FileRead(Inf1) If InLn1 == "*EOF*" Then Break InLn1 = StrReplace(InLn1, ",", "|") If ItemCount(InLn1, "|") <9 Then Continue If StrLen(InLn1) < 46 Then Continue I1 = ItemExtract(1, InLn1, "|") CurShr = StrFixChars(I1, " ", 30) I2 = ItemExtract(4, InLn1, "|") LCode = StrFixChars(I2, " ", 1) I3 = ItemExtract(5, InLn1, "|") CurUse = StrFixCharsL(I3, " ", 5) I4 = ItemExtract(6, InLn1, "|") Lmt = StrFixCharsL(I4, " ", 14) OutLn1 = StrFixChars(StrCat(CurShr, LCode, CurUse,Lmt), " ", DLen) FileWrite(Out1,OutLn1) EndWhile FileClose(Inf1) FileClose(Out1) SF1 = FileSize(Tmp2) FB1 = BinaryAlloc(SF1) BinaryRead(FB1, Tmp2) Delay(1) BinarySort(FB1, DLen, 0, 30, @STRING) Delay(1) BinaryWrite(FB1, Tmp3) ;;;;; Leave at this point since the data is now corrupt after the sort EXIT
tmp2="c:\temp\thefile.xxx" tmp3="c:\temp\thefile.yyy" SF1 = FileSize(tmp2) FB1 = BinaryAlloc(SF1) BinaryRead(FB1, Tmp2) Delay(1) BinarySort(FB1, 50, 0, 30, @STRING) Delay(1) BinaryWrite(FB1, Tmp3)
Sorting it with a record size of 50 would cause great problems. The resultant file would appear corrupted, because your cursor would be stepping through the file working with the 50 fixed character length, but getting off-sync when it steps over the CRLFs (which you didn't account for in your recsize).
Is this a bug? If so, where do I download the patch?
Basically it is sorting using the entire allocated buffer, and all you data moved up to the top of the buffer, and all the nulls are down below.
There are two ways to deal with it:
Input: 012121003010011 Desired result: 003010011012121 BinarySort result: 012003010011121the desired result and the result which is returned by the BinarySort function.
Running the script below, shows the problem
input='012121003010011' Length=StrLen(input) binbuf=BinaryAlloc(Length) for i=1 to Length number=StrSub(input,i,1) BinaryPoke(binbuf,i-1,number) next ; how to sort binbuf ? No ; flag "Sort Direction | Key Type" ; yields a sorted buffer ! sort_sequence=@ASCENDING BinarySort(binbuf,3,0,3,sort_sequence | @WORD1) byte_result="" for i=0 to Length-1 byte_result=StrCat(byte_result,BinaryPeek(binbuf,i)) next BinaryFree(binbuf) ; ; as a string, input is sorted correctly: ; binbuf=BinaryAlloc(Length) BinaryPokeStr(binbuf,0,input) BinarySort(binbuf,3,0,3,sort_sequence) string_result=BinaryPeekStr(binbuf,0,BinaryEodGet(binbuf)) BinaryFree(binbuf) ; ; Report on Sorted buffer below. ; result_text=StrCat("Input was ",@TAB,input,@CRLF,"Result should be: ",@TAB,string_result,@CRLF,"Result is: ",@TAB,@TAB,byte_result) if sort_sequence==@ASCENDING then sort_sequence="ASCENDING" else sort_sequence="DESCENDING" Message(StrCat("Results. Sort Sequence: ",sort_sequence),result_text) exit
I am running WinBatch Version - 2002A. What is the problem?
I can see what you want to do, but there are several issues with the approach. A datatype of WORD1 is a single 8 bit byte. But you are trying to sort 3 bytes with it. Not going to work. You maybe need 3 sorts to get it.
In addition a 3 byte field is not a native machine numeric data type. You can have 1, 2 or 4 byte numbers. And to further confuse the issue the bytes have to be placed in the buffer in the Intel numeric format (ummm low byte first)
Maybe you do really just want a string sort.
Article ID: W12728
Filename: BinarySort Considerations.txt
File Created: 2002:02:13:15:29:58
Last Updated: 2002:02:13:15:29:58