I am trying to recursively traverse through a directory and list any files that end in .txt or the names of folders.
void listFilesRecursively(char *basePath) {
char path[1000];
struct dirent *ptr;
DIR *dir = opendir(basePath);
// Unable to open directory
if (!dir)
return;
while ((ptr = readdir(dir)) != NULL)
{
if (strcmp(ptr->d_name,".") != 0 && strcmp(ptr->d_name, "..") != 0)
{
char *name = NULL;
char *type = NULL;
while (name != "." || name != NULL) {
name++;
}
for (name = ptr->d_name; *name != '\0' ; name++) {
if (name == '.') {
while (*name != '\0') {
*type = *name;
name++;
}
break;
} else if (*name == '\0') {
printf("%s\n", ptr->d_name);
}
}
if (strcmp(type,"txt") == 0)
printf("%s\n", ptr->d_name);
// create new path from our base path
strcpy(path, basePath);
strcat(path, "/");
strcat(path, ptr->d_name);
listFilesRecursively(path);
}
}
closedir(dir);
}
My expected results would be the files that end in .txt and files that are folders. However, the output is blank and it may go into an infinite loop.