Previous Table of Contents Next


5.2.11.4. Listing Files in a Directory

Listing files on the drive—in other words, being able to distinguish wildcards in DOS—is 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 Pascal’s DOS unit and the system unit.

System

procedure ChDir(dir:string); { changes directory to dirname in string }
procedure Erase(filename: <filevar>); { erases file var }
procedure FilePos(file: <filevar>); { current position in file }
procedure Filesize(file: <filevar>); { returns size of file }
procedure GetDir(drive;path: string); { gets current directory of
drive listed }
procedure MkDir(path: string); { makes directory }
procedure Rename(file: <filevar>); { renames file }
procedure RmDir(path: string); { removes directory }
procedure Seek(file: <filevar>); { moves position in file to read }

DOS

function Diskfree(diskno: byte): longint; { works as intended, ineffective for drives }
function Disksize(diskno: byte): longint; { > 1 GB }
var dosversion: word; { if DOS 6.22, Hi(dosversion) = 22, Lo(dosversion) = 6 }
var EnvCount: integer; { total number of environment variables }
function EnvStr(index: integer):string; { returns the env. string designated as index }
function FSearch(filename:PathStr; dirlist: string):pathstr; { searches for filename in path given by dirlist }
procedure FSplit(Path: PathStr; dir: dirstr; name: namestr; ext: extstr); { splits a filename up into a directory, name, and extension }
procedure Getdate(year, month, day, dayofweek: word); { Returns current date as set in the operating system }
function Getenv(stringname: string):string; { returns environment variable specified by name }
procedure GetFattr(file: <filevar>; attr: word); { obtains the attributes of a file in F }
procedure GetFtime(file: <filevar>; time: longint); { obtain packed date and time of file }
procedure GetTime(Hour, Minute, Second, Sec100: word); { gets current time in OS }
procedure PackTime(dnt: datetime; time: longint); { packs datetime record into time longint }
procedure SetDate(year, month, day: word); { sets date in OS }
procedure SetFTime(file: <filevar>; time: longint); { sets file to time specified }
procedure SetTime(hour, minute, second, sec100: word); { sets time in OS }
procedure UnPackTime(time: longint; dnt: datetime); { unpacks longint time to datetime record }

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