how do i get the file names of all the files in a folder / directory in c++?
Printable View
how do i get the file names of all the files in a folder / directory in c++?
There are no standard C++ functions to get the names of the files in a directory. You need to tell us the operating system and compiler you are using, since getting the names of the files all depend on compiler and OS.Quote:
Originally posted by kalavathy
how do i get the file names of all the files in a folder / directory in c++?
If you want a cross-platform solution, you can investigate the boost libraries at www.boost.org. They have a file/directory class that works on various operating systems.
Regards,
Paul McKenzie
In addition to Paul's correct answer...if you are using Windows, take a look at the following FAQ. It shows how one can do it in Windows, however, it basically can be easily ported to a different operating system/compiler since the way is still the same (except for the keywords probably)...
Dear Andreas,
Although the code in the link that u mention is quite perfect, but I saw another thread, which mentioned a minor bug in it, I mean...
like, it doesn't catch files like "this.is.my.file.txt" and I guess the solution was to use reverse iterator or _splitpath(...).Code:// Check extension
strExtension = FileInformation.cFileName;
strExtension = strExtension.substr(strExtension.find(".") + 1);
I just came through this thread and found that the FAQ is not updated, so just mentioned it, nothing personal ;)
Well...I would not consider this as a bug since I am still of the opinion that filenames like "this.is.my.file.txt" are invalid.Quote:
Originally posted by Ejaz
Although the code in the link that u mention is quite perfect, but I saw another thread, which mentioned a minor bug in it...
Anyway, this can be solved by using the 'rfind()' function instead of 'find()'. Since I do not want to start a discussion about whether these names are invalid or not, I simply changed the appropriate FAQs... :cool: