Function 4Eh            Find First File (FIND FIRST)

Searches a directory for the first file or directory whose name
and attributes match the specified name and attributes.

Entry AH = 4Eh
CX = File attributes
DS:DX = Pointer to ASCIIZ filespec (* ? allowed)

Return FileInfo structure at current DTA
or
AX = Error code, if CF is set
| 02h File not found
| 03h Path not found
| 12h No more files to be found

──────────────────────────────────────────────────────────────────

This function searches for the first filename matching the filespec
(ASCIIZ string pointed to by DS:DX). The filespec may include a drive
letter and path, and the filename can include wildcard characters.

The CX register controls the scope of the search by specifying a
file attribute mask. Note that bits 0 and 5 of the mask are
ignored, hence a search for files with attr_readonly or
attr_archive must be performed by testing these bits for all files.
If a program specifies any combination of attr_system,
attr_hidden, and attr_directory, this function returns normal
files in addition to the specified files. The program must examine
the attributes contained in the returned FileInfo structure to
determine the type of file found.


If the DTA has not been explicitly set by function 1ah, DOS uses
the default DTA at offset 80h in the program segment prefix (PSP).

──────────────────────────────────────────────────────────────────

Example

DATASEG
fname db "\abc\*.*",0 ; Search all files/directories in \abc
UDATASEG
fi FileInfo ? ; Uninitialized structure
CODESEG
assume ds:dgroup
mov dx,offset fi ; ds:dx -> new DTA
mov ah,1ah ; Set disk transfer address
int 21h
mov dx,offset fname ; ds:dx -> ASCIIZ filespec.
mov cx,ATTR_ANY - ATTR_VOLUME ; Exclude volume label
; from the search
mov ah,4eh ; Invoke Find First File
int 21h
jc @@cf_set ; Carry flag set if error
@@got_name:
mov bx,offset fi ; ds:bx -> FileInfo
test [byte bx + 15h],ATTR_DIRECTORY
jnz @@findnxt ; Skip if subdirectory
cmp [byte bx + 1eh],"." ; or parent's or own dir.
jz @@findnxt
lea dx,[bx + 1eh] ; ds:dx -> filename
call process_file ; Do something useful
@@findnxt:
mov ah,4fh ; Invoke Find Next File
int 21h
jnc @@got_name ; Back for more
@@cf_set:
cmp ax,12h ; No more files?
jz @@end ; Then return
call error_proc ; else trap error
stc
@@end:
ret