Searching All Directories on an HFS Volume
Searching All Directories on an HFS Volume How to and why not
Under some circumstances, it may be necessary to search an entire volume.
Remember that with the advent of large CD-ROMs and other large storage
mediums, this may be a time consuming process, especially over a network.
Apple recommends relying on files being in specific directories (such as the
same directory as the application, or in the "blessed folder”) or on having the
user find files with SFGetFile.
The example given below recursively calls PBGetCatInfo to get information
about every file and folder on a volume. You can use this code to do a partial
search of a volume by specifying a starting dirID other than fsRtDirID.
Example
// Assumes inclusion of MacHeaders
#include
void EnumerateCatalog(long);
HFileInfo myCPB; // for the PBGetCatInfo call
char fName[256]; // buffer to hold file names
short TotalFiles = 0;
short TotalDirectories = 0;
main()
{
WindowPtr oldWindow, MyWindow;
Rect myWRect;
myCPB.ioNamePtr = (unsigned char *)fName;
myCPB.ioVRefNum = 0;
EnumerateCatalog(fsRtDirID); // start at the root
// done with recursive call to print all file names on disk
printf("Total Files = %d\n",TotalFiles);
printf("Total Directories = %d\n",TotalDirectories);
}
void EnumerateCatalog(long dirIDToSearch)
{
short index = 1; // for ioFDirIndex
OSErr err;
do {
myCPB.ioFDirIndex = index; // set up index
// do this every time since PBGetCatInfo returns ioFlNum
// in this field
myCPB.ioDirID = dirIDToSearch;
err = PBGetCatInfo(&myCPB, false);
if(err == noErr) {
PtoCstr((char *)fName);
printf("%s\n",fName);
// check to see if the file is a folder
if(((myCPB.ioFlAttrib >> 4) & 0x01) == 1) {
// found a directory
TotalDirectories += 1;
EnumerateCatalog(myCPB.ioDirID);
err = 0;
} else
// found a file
TotalFiles += 1;
index += 1;
}
} while(err == noErr);
}