Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [Win32] FindFirstFile - FindNextFile returning name of directory

This thread is locked; no one can reply to it. rss feed Print
[Win32] FindFirstFile - FindNextFile returning name of directory
North~
Member #12,192
August 2010

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;

--------------------------------------------------
http://blog.wolfire.com/2009/04/always-initialize-your-memory/

If this is possible if you don't correctly initialize memory, it should be a priority of the highest order.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

North~
Member #12,192
August 2010

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.

--------------------------------------------------
http://blog.wolfire.com/2009/04/always-initialize-your-memory/

If this is possible if you don't correctly initialize memory, it should be a priority of the highest order.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

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~
Member #12,192
August 2010

That fixed it, thank you.

--------------------------------------------------
http://blog.wolfire.com/2009/04/always-initialize-your-memory/

If this is possible if you don't correctly initialize memory, it should be a priority of the highest order.

Go to: