Carbon - Getting list of files in a folder

Moderator
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
Moderator
Posts: 1,560
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.
Code:
#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
Quote this message in a reply
Member
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): http://www.inf.uni-konstanz.de/~kuehl/c+...ctory.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.
Quote this message in a reply
Post Reply