From: cklarson@burrito.engr.ucdavis.edu (Christopher Klaus Larson)
Newsgroups: comp.sys.mac.programmer
Subject: Re: Access directory contents
Date: 18 Nov 92 21:02:45 GMT
Distribution: comp.sys.mac.programmer
Organization: College of Engineering - University of California - Davis

In article <1992Nov16.131617.2096@ncsu.edu> cpastore@unity.ncsu.edu (Chris Pastore) writes:
>
>Is there some easy way to get the listing of file names once a
>directory is identified?
>

Call PBGetCatInfo on the directory: once with ioFDirIndex negative, to
obtain the number of files in the directory; then call it again, once for
each value of ioFDirIndex between 1 and the number of files in the
directory, inclusive. (I know this explination is not very clear, 
basically you are performing an indexed search of the directory.)

Source code for this method follows.

This can also be accomplished with PBCatSearch, although for using
PBGetCatInfo is easier for this task (IMHO of course :-)) and probably
faster in this case, since we don't have to search the entire volume.

Both PBGetCatInfo and PBCatSearch are explained in IM:Files.

--Chris
cklarson@engr.ucdavis.edu

Source (THINK C) follows, as always, comments, bug fixes, etc. welcome
-------------

/*******************************************************

   GetAllFiles -- Given a volume reference # (volume)
                  and a directory ID (directory)
                  this builds a FSSpec for each file
                  (NOT Directories) within the
                  given directory. NOTE: it does
                  nothing with the FSSpec -- that is
                  for you to add.

*******************************************************/
void GetAllFiles (short volume, long directory)
{
	CInfoPBRec		pb;
	short			num_files,index;
	Boolean			isDir,ignored;
	FSSpec			current_file;
	Str255			directory_name;
	
	/* Call once to determine # of files. Note that the name
	   of the directory is returned in directory_name. The
	   variable only exists to provide space for the returned
	   value -- we don't need the name for anything. */

	pb.dirInfo.ioCompletion = 0;
	pb.dirInfo.ioDrDirID = directory;
	pb.dirInfo.ioVRefNum = volume;
	pb.dirInfo.ioNamePtr = directory_name;
	pb.dirInfo.ioFDirIndex = -1;
	
	if (PBGetCatInfo (&pb,false))
		return; /* return on error */
	
	/* pb.dirInfo.ioDrNmFls now contains the number of files in our
	   directory */

	num_files = pb.dirInfo.ioDrNmFls;

	/* Now get info for each file in the directory, one at a time */

	for (index = 1;index <= num_files; index++)
		{

		/* The volume reference # and directory could be changed
		   by the call to ResolveAliasFile() later in the loop,
		   so they must be reset each time through to be correct. */

       		current_file.parID = directory;
	        current_file.vRefNum = volume;

		/* Set up Paramblock for indexed call...*/

		pb.hFileInfo.ioCompletion = 0;
		pb.hFileInfo.ioNamePtr = current_file.name;
		pb.hFileInfo.ioVRefNum = volume;
		pb.hFileInfo.ioFDirIndex = index;
		pb.hFileInfo.ioDirID = directory;
		
		/* Make the call and return on an error. */

		if (PBGetCatInfo (&pb,false))
			break;
		
		/* If the file is an alias, this will resolve it,
		   otherwise it will have no effect. */

		ResolveAliasFile (&current_file,true,&isDir,&ignored);

		/* If it's a directory, ignore it */
		if (!isDir)
			{
			
			/* At this point, current_file contains information
			   about one of the files in our directory. Do
			   whatever is necessary to the file here. */

			}
		}
}
Newsgroups: comp.sys.mac.programmer