Hi All,
I want to be able to iterate through a directory finding files with a specific file extension, like searching /home/username for all .sh files.
The language im using is C++.
Thanks
This is a discussion on All files in a directory within the Linux Programming forums, part of the Platform Specific Boards category; Hi All, I want to be able to iterate through a directory finding files with a specific file extension, like ...
Hi All,
I want to be able to iterate through a directory finding files with a specific file extension, like searching /home/username for all .sh files.
The language im using is C++.
Thanks
Well i guess you could use the directory reading functions in the POSIX c library.
I havnt tested it but that should list all the files in a directory. You can use ent->d_name and check it's extension.Code:#include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <sys/stat.h> void ListFiles(const char* path) { DIR *dir; struct stat statx; struct dirent *ent; if ((dir = opendir(path))) { while ((ent = readdir(dir))) { char fullName[1024]; strncpy(fullName, path, sizeof(fullName)); strncat(fullName, ent->d_name, sizeof(fullName)); stat(fullName, &statx); if (!(statx.st_mode & S_IFDIR)) //remove this if you also want to list sub directories printf("%s\n", fullName); } } closedir(dir); }