Win32: How do I get the name of files in a folder ?

To get the name of files in a folder,

1. call the FindFirstFile function to open a search handle and get information about the first file that the file system find in the folder.

2. call the FindNextFile function to continue listing files from a previous call to FindFirstFile.

WIN32_FIND_DATA FindFileData;
HANDLE hFind;
TCHAR *FilePathBuff = L"C:\\RequiredFolder";
hFind = FindFirstFile(FilePathBuff, &FindFileData);

if (hFind == INVALID_HANDLE_VALUE)
{
   printf(TEXT("FindFirstFile failed (%d)\n"), GetLastError());
}
else
{
   printf(TEXT("The first file is %s\n"), FindFileData.cFileName);
   while (FindNextFile(hFind, &FindFileData) != 0)
   {
      printf(TEXT("The next file is %s\n"), FindFileData.cFileName);
   }
   FindClose(hFind);
}
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s