;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfFilePickRandomLine (strFilename) If strFilename == "" Then Return "" intBBSize = FileSize (strFilename) If intBBSize == 0 Then Return "" hdlBB = BinaryAlloc (intBBSize) BinaryRead (hdlBB, strFilename) ; Count how many lines. intLineCnt = BinaryStrCnt (hdlBB, 0, intBBSize - 1, @CRLF) -1 While @TRUE ; Pick a random line (first line is line 0). intRandLineNum = Random (intLineCnt) ; Go find that line. intLineStartsAt = 0 For intLine = 1 To intRandLineNum intLineStartsAt = 2 + BinaryIndex (hdlBB, intLineStartsAt, @CRLF, @FWDSCAN) Next ; Found beginning of line. Find end. iLineEndsAt = BinaryIndex (hdlBB, intLineStartsAt, @CRLF, @FWDSCAN) ; Extract Line. strLine = BinaryPeekStr (hdlBB, intLineStartsAt, iLineEndsAt - intLineStartsAt) If strLine != "" Then Break EndWhile hdlBB = BinaryFree (hdlBB) Return strLine ;.......................................................................................................................................... ; This UDF "udfFilePickRandomLine" returns a single line, ; randomly extracted out of textfile. ; This can be used for building a "quote of the day" application. ; ; Conf: WinBatch ; From: Marty marty@winbatch.com ; Date: Sunday, December 02, 2001 09:49 PM ; Slightly modified by Detlev Dalitz.20020204 ;.......................................................................................................................................... #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfFilePickRandomLineV2 (strFilename) If strFilename == "" Then Return "" strItemList = FileGet (strFilename) If strItemList == "" Then Return "" intLineCnt = ItemCount (strItemList, @LF) - (StrSub (strItemList, FileSize (strFilename), 1) == @LF) While @TRUE strLine = ItemExtract (1, ItemExtract (1 + Random (intLineCnt), strItemList, @LF), @CR) If strLine != "" Then Break EndWhile Return strLine ;.......................................................................................................................................... ; This UDF "udfFilePickRandomLine" returns a single line, ; randomly extracted out of textfile. ; This can be used for building a "quote of the day" application. ;.......................................................................................................................................... ; Detlev Dalitz.20030709 ;.......................................................................................................................................... #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strFilename = IntControl (1004, 0, 0, 0, 0) ; We use this file as test input. strMsgTitle = "Demo: udfFilePickRandomLine (strFilename)" strMsgText = udfFilePickRandomLine (strFilename) Message (strMsgTitle, strMsgText) strMsgTitle = "Demo: udfFilePickRandomLineV2 (strFilename)" strMsgText = udfFilePickRandomLineV2 (strFilename) Message (strMsgTitle, strMsgText) Exit ;------------------------------------------------------------------------------------------------------------------------------------------