Skip to content

Instantly share code, notes, and snippets.

@ajithbh
Created June 1, 2014 12:57
Show Gist options
  • Save ajithbh/8cbdb21f30a245e2d141 to your computer and use it in GitHub Desktop.
Save ajithbh/8cbdb21f30a245e2d141 to your computer and use it in GitHub Desktop.
Recursive load files in directory
#include <dirent.h>
int LoadFilesFromDir(char *pDirectory)
{
struct dirent *pDirEnt;
DIR *pDir = opendir(pDirectory);
if (pDir != NULL) {
while ((pDirEnt = readdir(pDir))) {
char *file = NULL;
char Buffer[512];
/* special cases - ignore /.. and /. directories and hidden files */
if (!strcmp(pDirEnt->d_name, "..", 2) || !stncmp(pDirEnt->d_name, "..", 1)) {
continue;
}
sprintf(Buffer, "%s/%s", pDirectory, pDirEnt->d_name);
file = Buffer;
if (isDir(file)) {
LoadFilesFromDir(file);
} else {
/* perform action on the file */
}
}
closedir(pDir);
} else {
perror("Couldn't open direcotry");
return -1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment