Carbon - Getting list of files in a folder

Posts: 133
Joined: 2008.05
Post: #1
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?
Quote this message in a reply
Posts: 1,563
Joined: 2003.10
Post: #2
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.

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 {
      itemList = NULL;
  return itemList;

- Alex Diener
Quote this message in a reply
Posts: 184
Joined: 2004.07
Post: #3
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):

(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.
Quote this message in a reply
Post Reply