Skip to main content
gamedev.net
🔒 Locked

Retrieving folder contents

Started by Sander 3 replies 450+ views
Original Post
Sander
Sander
In my game, I want to load all the filenames within a specific folder into an array. Have you any idea how I should approach this? I use Win98 and Visual C++. I need this so I can get a list of all available levels in my game (each level is a separate file). Any ideas are greatly appreciated. I know OpenGL but I suck at windows stuff... Sander Maréchal [Lone Wolves Production][Articles][GD Emporium][E-mail]
mutex
mutex
Use Win32''s FindFirstFile and FindNextFile functions:


  
std::list<std::string> filelist;
WIN32_FIND_DATA data;
HANDLE hFind = FindFirstFile("c:\\temp\\*.lvl", &find);
if (hFind == INVALID_HANDLE_VALUE) { printf("you suck\n"); return; }

do
{
if (strcmp(data.cFileName, ".") == 0 || strcmp(data.cFileName, "..") == 0) continue;

filelist.push_front(std::string(data.cFileName));
} while (FindNextFile(hFind, &data);
FindClose(hFind);


I''m not sure if the code will work, but that''s the general gist.

Beer Hunter
Beer Hunter
Similar to what mutex said:

  
// eg: fileList("levels\\*.lvl")

std::list<std::string> fileList(const char *mask)
{
std::list<std::string> result;
WIN32_FIND_DATA findData;
HANDLE findHandle = FindFirstFile(mask, &findData);
if (findHandle == INVALID_HANDLE_VALUE) return result;

do {
if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
result.push_back(findData.cFileName);
} while (FindNextFile(findHandle, &findData));
FindClose(findHandle);

return result;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.