The QBNews Page 3 Volume 1, Number 1 November 1, 1989 ---------------------------------------------------------------------- W h o y a g o n n a c a l l ? C A L L I N T E R R U P T ---------------------------------------------------------------------- QuickBASIC Directories by Hector Plasmic While QuickBASIC has a lot of features, there are a few it lacks. Ever wanted to do a directory search? Chances are you had to Shell to DOS, create a temporary file by redirecting the DIR command, then return to QB and read the file line-by-line, parsing out the filenames. Either that or you resorted to a third-party library function. It's not necessary. QB can be cajoled into doing directory searches by utilizing the DOS directly via Interrupt. Interrupt 21h functions 4Eh and 4Fh do find-first-match and find-next-match respectively. If the call to these functions is successful, information on the matched file is placed by DOS into the current DTA (disk transfer area) as follows: Bytes 0-20 = (reserved, used during findnext calls by DOS) Byte 21 = attribute of matched file Bytes 22-23 = file time Bytes 24-25 = file date Bytes 26-27 = least significant word of file size Bytes 28-29 = most significant word of file size Bytes 30-42 = filename and extension in form of ASCIIZ string All this brings up another question: where is the current DTA? The answer is given by a call to interrupt 21h function 2Fh, which returns the DTA's offset in BX and the segment in ES. So, we make a call to interrupt 21h with register .ah set to 2Fh like this: InRegs.AX = &H2F * 256 'Remember, .ah is the high-byte of .ax INTERRUPTX &H21, InRegs, OutRegs And retrieve the returned information: DTASeg = OutRegs.ES 'Segment of current DTA DTAOff = OutRegs.BX 'Offs of current DTA If we now say DEF SEG = DTASeg we can directly PEEK into the DTA, wherever it may be. Okay, we know where the data will be, now how do we get it there? Like this: FLName$ = "*.*" + CHR$(0) 'ASCIIZ filespec (null terminated) InRegs.AX = &H4E * 256 'Which function (4Eh) InRegs.CX = 0 'Attribute (normal) InRegs.DS = VARSEG(FLName$) 'Segment of ASCIIZ filename InRegs.DX = SADD(FLName$) 'Offset of ASCIIZ filename InterruptX &H21, InRegs, OutRegs The QBNews Page 4 Volume 1, Number 1 November 1, 1989 This calls interrupt 21h function 4Eh and places the information on the first matched file into the DTA. There's a catch, though. There may not be a matching file. If there isn't, this function returns an error by setting the carry flag and returning a code in AX. We can check for this with: IF OutRegs.Flags AND 1 THEN WhichError = OutRegs.AX The two possible errors are "invalid path" (2h) and "no (more) matching file(s)" (12h). Now all that remains is to PEEK the information out of the DTA and do something useful with it. FINDEM.BAS is a sample program together with a Function FindFirst that will do directory searches for you. Modify the main module code to load the filenames into an array or whatever you need it to do.