View Full Version : Carbon - Getting list of files in a folder
LongJumper
2005.03.31, 02:24 PM
I have a small application that converts .obj files to a binary version of it so it reads faster. Unfortunately, everytime I change the model or have to convert a new model I have to manually select each file(usually 10 per body) and convert them individually. I was wondering if there was a way to put all the names of files inside a single folder into an array or something so I could just iterate through them, as opposed to individually specifying them each time?
ThemsAllTook
2005.03.31, 02:30 PM
This is a function I wrote for Water Tower 3D. It returns an array of FSRefs, and the number of elements in the array in outItemCount. Pass the directory whose items you want to list in parentRef.
#define MAX_FILES_PER_FOLDER 1000
FSRef * listFilesInFolder(FSRef * parentRef, int * outItemCount) {
OSErr error;
FSIterator iterator;
ItemCount itemCount;
FSRef * itemList = NULL;
error = FSOpenIterator(parentRef, kFSIterateFlat, &iterator);
if (error == noErr) {
itemList = (FSRef *) malloc(sizeof(FSRef) * MAX_FILES_PER_FOLDER);
if (error == noErr) error = FSGetCatalogInfoBulk(iterator, MAX_FILES_PER_FOLDER, &itemCount, NULL, kFSCatInfoNone, NULL, itemList, NULL, NULL);
if (error == noErr || error == errFSNoMoreItems) {
*outItemCount = itemCount;
} else {
free(itemList);
itemList = NULL;
}
FSCloseIterator(iterator);
}
return itemList;
}
- Alex Diener
phydeaux
2005.03.31, 02:43 PM
I had previously used the source from this page with a nice POSIX way to do this (so you can do it in OS X): http://www.inf.uni-konstanz.de/~kuehl/c++/directory.html
(edit) Whoops, now I'm setting a bad example and not reading the post completely (Carbon.) However, I'm leaving this in case it's helpful to someone.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.