tags:

views:

396

answers:

1

I am using the Windows API and would like to be able to search through a specified directory and return the names of any files that reside within it.

I've made a start at it however i've hit a brick wall as i'm unsure of how to go any further.

Here is my progress so far:

#include <stdio.h>
#include <windows.h>

void main()
{
 HANDLE fileHandle;
 WIN32_FIND_DATAA fileData;

 fileHandle = FindFirstFileA("*.txt", &fileData);

 if(fileHandle != INVALID_HANDLE_VALUE)
 {
  printf("%s \n", fileData.cFileName);
 }
}
A: 

You need to call FindNextFile in a loop to find all the files. There's a full example here, here are the interesting bits:

hFind = FindFirstFile(szDir, &ffd);

if (INVALID_HANDLE_VALUE == hFind) 
   return dwError;

do
{
   printf("%s\n"), ffd.cFileName);
}
while (FindNextFile(hFind, &ffd) != 0);
interjay
I tried using the example given on MSDN but it seems to close as soon as it opens.
Jamie Keeling
Try running it from the command line and see what it says.
interjay
I managed to get a snippet of text from the console:\Release\Exercise 2.exe <directory name>Press any key to continue . . .I've omitted the directories leading up to the .exe
Jamie Keeling
It's telling you how to use the program. You need to supply a directory name as a command line parameter. Or instead, you can incorporate the code I posted into your program - just change the variable names to the ones you used, and you may need to use FindNextFileA instead of FindNextFile.
interjay
Thank you for the help
Jamie Keeling