Sign in to follow this  

Getting file-names in a directory

Recommended Posts

Hi! I'm searching for a method/algorithm or a way how to get every filename in a specified folder. I used the search function, but the only thing I found was "outdatet" because dirent.h doesnt exists (anymore?). Also searched articles on gamedev but found nothing that could help me. At the moment I'm using ms visual c++ 2005. thanks in advance

Share this post

Link to post
Share on other sites
A short question:
My source looks like this:

BOOL xyz::GetFilesFromDirectory(char* Path){
string searchPath = Path;
HANDLE fileSearcher;
BOOL worked = FALSE;
LPWIN32_FIND_DATA DataInformation = NULL;

fileSearcher = FindFirstFile(Path, DataInformation);

if(fileSearcher != INVALID_HANDLE_VALUE && DataInformation != NULL){
worked = TRUE;

while(FindNextFile(fileSearcher, DataInformation)){

return worked;

The path is:

The problem is that he doesnt find any file.
In the documentaition is written that you can use wildcars for files and some other stuff.
Tried with the path:
But if I use this path I always get an unhandled exception.

Any suggestions?

Share this post

Link to post
Share on other sites
You need to allocate your DataInformation variable, not pass a NULL pointer.

WIN32_FIND_DATA theFind;

hFind = FindFirstFile(Path,&theFind);
return false;

do {
// Add filename to list
} while(FindNextFile(hFind,&theFind);

FindClose(hFind); // Don't forget this!

You want to pass "Data\\Maps\\*.dat" as your path. If you pass "Data\\Maps", then there won't be any files that match that path, so FindFirstFile() returns INVALID_HANDLE_VALUE, and DataInformation is never filled. When you pass "Data\\Maps\\*.dat", DataInformation gets used, and you get an access violation as Win32 tries to write to a NULL pointer.

[Edited by - Evil Steve on March 5, 2006 1:00:55 PM]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this