Previous | Table of Contents | Next |
5.2.11.4. Listing Files in a Directory
Listing files on the drivein other words, being able to distinguish wildcards in DOSis a multistep process. The components of performing this function, the predefined searchrec record type, findfirst, findnext, and the file attribute constants are described in this section.
The definition of searchrec, which is defined in the DOS unit, is as follows:
searchrec = record fill: array[1..21] of byte; attr: byte; time: longint; size: longint; name: string[12]; end;
fill represents a filler area, for which there is no purpose to access, and attr specifies the file attributes of the file described, stored additively with respect to the file attribute constants described later. time is the date and time in the standard DOS-packed format. The process of unpacking this variable is described later. size represents the size in bytes of the file, and name represents the name of the file.
findfirst and findnext are called in a manner described in Listing 5.45, using a variable defined as searchrec earlier. findfirst takes a path, attribute type, and searchrec variable and returns the first file that fits the description. A subsequent call to findnext returns the next file that fits the original description.
File attribute constants, which may be referred to in any description of file attributes, are the following:
ReadOnly := $01 Hidden := $02 SysFile := $04 VolumeID := $08 Directory := $10 Archive := $20 AnyFile := $3F
Listing 5.45. A demonstration of findfirst and findnext.
program fig45; uses dos; var fileinfo: searchrec; begin findfirst(*.*,AnyFile, fileinfo); while doserror = 0 do { doserror = 18 = no more files to list } begin write(fileinfo.name); if (fileinfo.attr and Directory) = Directory then writeln([DIR]:18) else writeln(fileinfo.size:18); findnext(fileinfo); end; end.
5.2.11.5. DOS Procedures and Functions
This section provides a listing of the procedures and functions that are equivalent to DOS capabilities in Turbo Pascals DOS unit and the system unit.
System
DOS
Procedures that need further explanation than what can be obtained from the preceding list are further described. For any spots at which a file variable occurs, the file must be assigned but unopened. The longint time variable is a packed longint. To be read or set, it must be unpacked and packed. The datetime record is predefined for that purpose. It is as described here:
datetime = record year, month, day, hour, minute, second: word;
In most of these functions and procedures, the variable doserror is set away from 0, which may be used to check for the successfulness of the statement issued. Otherwise, if this is not the case, the IOResult variable method may be used. Also, the DiskSize and DiskFree functions take a numerical value for a drive, where 0 = current drive, 1 = A, 2 = B, 3 = C, and so forth.
Listing 5.36 shows an example of a good way to copy a file. Moving a file on a drive can be accomplished by renaming the file (it works on paths), or by copying the file and then deleting it across drives.
Previous | Table of Contents | Next |