[Win32] FindFirstFile - FindNextFile returning name of directory
North~

I'm trying to find all the files in a directory named "en" and no matter how many times I search through with FindNextFile, it always returns the same thing. "en" which is not a file or directory inside of \en. Anyone see a problem?

#SelectExpand
1if (!isADirectory(argv[1])) 2 { 3 printf("%s is not a directory.",argv[1]); 4 exit(1); 5 } 6 else 7 { 8 printf("%s is a directory.\n",argv[1]); 9 } 10 11 WIN32_FIND_DATA wData; 12 HANDLE wHandle; 13 14 wHandle = FindFirstFile(argv[1],&wData); 15 16 printf("First file in directory is: %s\n",wData.cFileName); 17 18 if (wHandle == INVALID_HANDLE_VALUE) 19 { 20 printf("Error in finding a directory."); 21 } 22 23 FindNextFile(wHandle, &wData); 24 printf("Second file in directory is: %s\n",wData.cFileName); 25 FindNextFile(wHandle, &wData); 26 printf("Third file in directory is: %s\n",wData.cFileName); 27 28 string fullpath_s = argv[1]; 29 fullpath_s += "\\"; 30 fullpath_s += wData.cFileName;

Edgar Reynaldo

From looking at
FindFirstFile
FindNextFile
WIN32_FIND_DATA
it seems you are using the functions properly.

So, that leaves the problem up to the path you are passing into your code as argv[1].

According to the docs, it seems you should be using a path like this :

drive:\\folder\\en*

North~

I have tried this before with single and double backspaces and the result is the same

First file: en
Second file: en
Third file: en

I am aware that the first two things in a folder are references to the folder itself "." and the parent ".." but even if it was picking up these, the second "file" should be "Desktop" and the third should be the first file in the actual folder. However, this is not the case, and I am confused.

Edgar Reynaldo
North~ said:

I'm trying to find all the files in a directory named "en" and no matter how many times I search through with FindNextFile, it always returns the same thing. "en" which is not a file or directory inside of \en. Anyone see a problem?

I misread what you said earlier. I thought you were looking for all the files starting with 'en', not for all the files in a directory named 'en'. You need to alter your search path to something like this :

drive:\\folder\\en\\*

(Only escape the backslashes if you aren't entering it in manually)

I assume before you were trying to search by using something like this :

drive:\\folder\\en

and that would only find the en folder, which is why it keeps showing up when you call FindNextFile.

Also, make sure you check the return value of FindNextFile. It will return false when there are no more matching files and then calling GetLastError will return ERROR_NO_MORE_FILES.

Since you're asking the user for a directory, you need to append a backslash if there isn't one, and an asterisk as a wildcard to match all files and folders within the directory specified.

North~

That fixed it, thank you.

Thread #607839. Printed from Allegro.cc