Jump to content
  • Advertisement
Sign in to follow this  
K A Z E

How to find every file of a particular type in a directory

This topic is 4612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a 2D game. And I want to make an editor for it where you can make modules like neverwinter nights and then play them in the game. What I want to do is when you hit open module on the menu, I want it to bring up a list of all the module files that are in the Modules directory where my program is. So is there a way to like loop through all files in a certain directory or something? I mean I could use a text field and allow insertion of the module's name and then open that file, but then I would still need to know how to do this for the actual game, since I want there to be a module list when you hit new game like in NWN. So if anyone could tell me how to do this, I'd appreciate it. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
I would second the recommendation for boost::filesystem, but if you are looking for a lightweight alternative I have used code like this in the past:


#include <string>
#include <vector>
#include <windows.h>

typedef std::vector < std::string > FileList;

FileList getFilesInDir(const std::string & dir, const std::string & filter)
{
FileList fl;

std::string filename = dir + filter;

WIN32_FIND_DATA ffd;

HANDLE h = FindFirstFile(filename.c_str(), &ffd);

if (h == INVALID_HANDLE_VALUE) return fl;

do
{
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
fl.push_back(ffd.cFileName);
}
} while (FindNextFile(h, &ffd));

FindClose(h);

return fl;
}




Use:
FileList availableModules = getFilesInDir("modules/", "*.mod");

Alan

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!