3.5.1 File Management

A program can create a new file by using Create File with Handle (Interrupt 21h Function 3Ch). This function creates a file, gives it the specified name, places it in the specified directory on the specified drive (or in the current directory on the current drive, if a path is not given), and returns a handle for the file. The new file is initially empty (that is, it contains zero bytes), but it is opened for both reading and writing, so the program can write to it by using Write File or Device (Interrupt 21h Function 40h) and then read from it by using Read File or Device (Interrupt 21h Function 3Fh).

When a program creates a file, it sets file attributes that specify the type of access programs have to the file. These attributes can be any of the following:

Attribute Description

ATTR_READONLY (01h) Specifies a read-only file. Programs cannot write to the file.
ATTR_HIDDEN (02h) Specifies a hidden file. System commands such as dir do not list the file. Functions such as Find First File and Find Next File (Interrupt 21h Functions 4Eh and 4Fh) do not return information about the file unless the search specifies this attribute.
ATTR_SYSTEM (04h) Specifies a system file. This attribute is usually reserved for system files such as IO.SYS and MSDOS.SYS. This has the same effect as ATTR_HIDDEN and, when applied to program files, prevents COMMAND.COM from finding and running the files.
ATTR_ARCHIVE (20h) Specifies a file that is new or has been modified. The system automatically sets this attribute when the file is created or written to. The attribute does not affect access to the file but gives programs a quick way to check for potential changes to the file contents.

A file is a normal file (ATTR_NORMAL) if it has no other attributes. Programs have full access to normal files.

Note that, even if the program specifies the read-only attribute, a new file is always opened for both reading and writing, so that the program can write to the initially empty file. The read-only attribute does not take effect until after the file is closed for the first time.

A program can determine a file's attributes by using Get File Attributes (Interrupt 21h Function 4300h), and it can change them by using Set File Attributes (Interrupt 21h Function 4301h).

A program can retrieve a file's date and time by using Get File Date and Time (Interrupt 21h Function 5700h). MS-DOS initially sets the date and time when a file is created and updates them when a program writes to the file. A program can change the date and time for a file by using Set File Date and Time (Interrupt 21h Function 5701h).

A program can retrieve the name, attributes, time, date, and size of one or more files by using Find First File (Interrupt 21h Function 4Eh) and Find Next File (Interrupt 21h Function 4Fh). These functions search for files having names and attributes that match values supplied by the program. If the functions find files that match, they return information for the files in a FILEINFO structure. (For a full description of the FILEINFO structure, see Section 3.9, “Structures.”) If the name supplied by the program contains wildcards, the functions return information about all files that match the patterns. Wildcard searches are iterative—that is, the program calls Find First File and then repeatedly calls Find Next File until all files matching the name and attributes have been found.

Both Find First File and Find Next File copy the file information to the buffer pointed to by the disk transfer address (DTA). By default, MS-DOS sets the DTA to point to the last 128 bytes of the program segment prefix (PSP). (For information about the PSP, see Chapter 5, “Program Management.”) If this default buffer is not adequate, the program can change the DTA by using Set Disk Transfer Address (Interrupt 21h Function 1Ah). A program can retrieve the current DTA by using Get Disk Transfer Address (Interrupt 21h Function 2Fh).

A program can rename a file by using Rename File (Interrupt 21h Function 56h). This function replaces the name and extension in the directory entry with a new name and extension. All other information remains unchanged.

A program can also use Rename File to move files. If the program supplies a new path for the file, the function moves the file's directory entry from the old directory to the new one. However, the function cannot move a file from one drive to another.

A program can delete a file by using Delete File (Interrupt 21h Function 41h). This function frees any space on the drive that has been allocated for the file and marks the file's directory entry as deleted.