[comp.sys.ibm.pc] How to read an MS-DOS directory

psc@lzaz.ATT.COM (Paul S. R. Chisholm) (05/15/87)

< "I'm *not* expendable, I'm *not* stupid, and I'm *NOT* going!" >

Goodness, gracious!  Thanks, everyone, for all the replies.  (I can't
believe I got a reply from *Amsterdam* within twenty-four hours!)
Sorry that I posted the request twice.  I was trying to send it to
both comp.sys.ibm.pc and comp.sys.ibm.pc.digest.  I tried to cancel the
second one, really I did!

To recap:  You can't just "open" a directory with the appropriate DOS
function; you'll get a error to the effect that you don't have
permission to do that.

What everyone suggested is to use the "find first" and "find next" DOS
functions.  There are two pairs.  Functions 11h and 12h work only in
the current directory, but work in all versions of DOS.  Functions 4Eh
and 4Fh take a full path name, on DOS 2.0 and above.

Let's say you want want a list of all the files in "c:\bin\newstuff".
(Note that the C string constant for this path is "c:\\bin\\newstuff",
to avoid the "\b" and "\n" character escapes!)  You'd pass the string
"c:\\bin\\newstuff\\*.*" (or "c:\\bin\\newstuff\\????????.???") to
function 4Eh.  You can use the attribute information to specify what
kinds of files you want to see (read-only, hidden, system, volume,
subdirectory, archived or not); setting all the interesting bits gives
you everything.  The "Data Transfer Area" (DTA) will give you the name
of the file you've found, the attributes, the date and time stamps, and
enough information for DOS to determine the next file in the directory
with function 4Fh.  Eventually, you'll be told "no more files".

A C fragment might help here:

	for ( i = find_first( path, & dta, attr ); i == FOUND; i = find_next( & dta ) {
		process_file( & dta );
	}

I guess if you have enough places to store DTAs (or copies of the DTA),
you could write a simple recursive program to walk the tree.  If not,
you could do a breadth-first search, and store the directories you
haven't visited yet.

Thanks to Jon Wesener of the University of Wisconsin at Madcity, Philip
Freidin of Advanced Micro Devices, Conrad Pino of Pacific Bell, Robert
McFarlane of the University of Washington, Dave Tutleman upstairs, and
Irvin Shizgal of the AMOEBA project at CWI in Amsterdam for general
advice and offers of source code; Matthew Weinstein at UCLA for a
pointer to the "dirlib" package; Atul Kacker at the University of
Rochester Computing Center for offers of Turbo Pascal and Lattic C
source code; and special thanks to Mark D. Salzman of Tektronix at
Beaverton for a well documented, detailed page and a half of C source
that told me everything I needed to know.

I've discovered that on Monday, the C compiler I use will have a stat()
function that provides this information, too.  Why am I being so
cryptic?  I'll tell you on Monday.

-Paul S. R. Chisholm, UUCP {ihnp4,cbosgd,allegra,vax135,mtune}!lznv!psc
AT&T Mail !psrchisholm, Internet psc@lznv.att.com
I'm not speaking for my employer, I'm just speaking my mind.