Turbo Pascal 3.0 projects



Search files using Turbo Pascal

Turbo Pascal (CP/M-80) provides the procedures RESET(file) and REWRITE(file) for working with files After execution, Turbo Pascal calls an internal error routine. If an error occurs a message will be sent and the program is aborted. The programmer may avoid abortion, if a file to be opened is not found, by using the directive {$I-}RESET(file){$I+}. (But don't forget to handle a pending error e.g. by if IOresult<>0 then ...). This allows looking for a file, but with unambiguous file name only. Sometimes, it is necessary to search for multiple files with ambiguous file name, e.g. search for files of type .TXT, which be represented in CP/M as *.TXT.
Mallard BASIC has a built in command − FIND$(name,number) − handling multiple files. Here name means the mask for the files to be searched for and number is the number of the file to be fetched. On success, FIND$ returns an unambiguous file name. Otherwise, an empty string will be returned.

I will now describe a Turbo Pascal function which works the same way the Mallard BASIC function does. The base for searching files are two BDOS functions, namely Search for First (function 17) and Search for Next (function 18). Searching for first requires an CP/M File Control Block, subsequent calls to searching for next do not. On success, the BDOS functions return a number in the range 0..3, otherwise 255. The number is an index to one of four entries within the current DMA buffer. Because the size of the buffer is 128 bytes, this results in a 32-byte file information − exactly one directory entry. The first byte of this entry is the user area, followed by eight characters for the file name and three for the file extension.

The following CP/M structures are required for a file search:
  1. An FCB. Here, the default FCB may be used − starting at address 0x005C.
  2. A disk buffer. The default buffer − starting at address 0x0080 − cannot be used here because Turbo Pascal uses the memory − starting at address 0x00A0 − for internal storage.
The search function takes a string as input as well as for output. Therefore, a conversion must be performed from one structure to the other. The process of the function Finds(name,number) is as follows:
  1. The procedure StringToFCB converts the file name into an FCB structure.
  2. Then, searching takes place until the requested file is found or no more files are found.
  3. The procedure FCBtoString converts the FCB structure into the resulting file name.
Listing of the sample program
[Find here the module FINDS.INC and here the mentioned article "Turbo Directory"]

Compiled by Werner Cirsovius
July 2009