Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

wolfman8k

How can I get the files in a directory?

This topic is 6410 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 have a game that saves files to a directory, and I need to be able to open them, but I don''t know the names. I need something like this:
  

changedir("C:\Mygame");
num_of_files = get_file_count();
char filename[8][num_of_files];

for(i=0; i<num_of_files; i++)
{
filename<i>=name_of_file(i);
}

  
Thanks

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
This code will stick the names of all the files in the current directory (and recursively in subdirectories) into s_vecFilenames. (the files in subdirectories will have their full paths, like "Textures\Foo.tga") It skips over "." and ".." obviously (actually, any filename that begins with ".").

  

#include <windows.h>
#include <vector>
#include <string>
using namespace std;

void main()
{
EnumerateAllFiles("");
}

static vector<string> s_vecFilenames;
void EnumerateAllFiles(const char* szDir)
{
WIN32_FIND_DATA wfd;
memset(&wfd, 0, sizeof(WIN32_FIND_DATA));

char szPath[1024];
sprintf(szPath, "
%s*.*", szDir);

HANDLE hFile = FindFirstFile(szPath, &wfd);

if ( INVALID_HANDLE_VALUE != hFile )
{
do
{
if ( wfd.cFileName[0] == ''.'' )
{

}
else if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
char szDirChild[1024];
sprintf(szDirChild, "
%s%s\\", szDir, wfd.cFileName);
EnumerateAllFiles(szDirChild);
}
else
{
char szFilename[1024];
sprintf(szFilename, "
%s%s", szDir, wfd.cFileName);
s_vecFilenames.push_back(szFilename);
}
}
while ( FindNextFile(hFile, &wfd) );
}
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!