PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Listing files in a directory

I'm having trouble finding any information on how to get a list of files or filenames (preferably with specific extensions) from a given directory, not using MFC.

Any help appreciated...
Code:
WIN32_FIND_DATA fd;//structure to contain returned file info
HANDLE hFind;//file handle returned
// count the files to see how many, to size the box
TCHAR fullpath[MAX_PATH];
int pathlen = PocketPC::GetFullPathName(_T("A"),fullpath);
fullpath[pathlen-1]=0;//chop off the 'A' needed for fullpath func to work?
_tcscat(fullpath,TEXT("*.sav"));

int file_cnt=0;

hFind = FindFirstFile(fullpath, &fd);//TEST find first file of type *.sav
if (hFind != INVALID_HANDLE_VALUE)//if we found one
{
text_y+=16;//move down
file_cnt++;
}
for(int z=0;z<20;z++){//find up to a further 99 files
if(FindNextFile(hFind, &fd))// next file
{
file_cnt++;
if(z<9)
{
text_y+=16;//move down
}
}//end if
}//end for
FindClose(hFind);// stop finding files

this code snippet is specific to my game and contains some rubbish coding so ignore the stuff I'm using to size a box surrounding file names etc & just look at the findfirstfile and findnextfile bits. Check the docs for these & you should be ok
Thanks, looks good, I'll try it later today...
Here's the typical usage. This iterates everything in the root directory.

Code:

WIN32_FIND_DATA fd;
HANDLE hFind = FindFirstFile(_T("\\*.*"), &fd;);
if (hFind != INVALID_HANDLE_VALUE) {
do {
//Do stuff with fd here
} while (FindNextFile(hFind, &fd;);
FindClose(hFind);
}