FindFirstFile and FindNextFile

This is a discussion on FindFirstFile and FindNextFile within the Windows Programming forums, part of the Platform Specific Boards category; Before I ask my question, yes I have done searches. I've gone through this board, google, MSDN, and still don't ...

FindFirstFile and FindNextFile

Before I ask my question, yes I have done searches. I've gone through this board, google, MSDN, and still don't know what is happening.

I have part of my code checking to see if the program is in fact getting the file name, and it is not. I know this directory has only files, and no other directories. If I use FindFirstFile("*.*",&ffData) it will always return a directory for dwFileAttributes, and never give me a filename. If I use FindNextFile(".txt",&ffData) or something, it will then give me a file name. Why does "*.*" never return a file name?

Code:

_chdir("C:\\aaMyFiles"); //this is further up in the code
//from here down is all together
if((buffer = _getdcwd(3,lBuffer,MAX_PATH)) == NULL)
perror("_getcwd error");
hFind = FindFirstFile("*.*",&ffData);
if(ffData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
SetDlgItemText(Hwnd,TESTING,ffData.cFileName); //i never get the file name
else if(hFind == INVALID_HANDLE_VALUE)
SetDlgItemText(Hwnd,TESTING,"invalid handle"); //hasn't been a problem yet
else if (ffData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
SetDlgItemText(Hwnd,TESTING,buffer); //ALWAYS COMES TO THIS ONE
else
SetDlgItemText(Hwnd,TESTING,ffData.cFileName); //just in case, but still doesnt show a file name

Thank you for the help, but I do not see how that answers my question. I want to know why FindFirstFile() is not finding any files with "*.*" specified as the first argument. Shouldn't the wild card *.* mean find any file with any extension?

I tried your IsBitSet() and it works the exact same as before. Like I said, there are no directories or folders in C:\Music folder. There is only one file, and it cannot even retrieve the name.

Don't use any criteria to display the retrieved file - what does it display if you remove all the if statements and just print the file retrieved regardless of what it is ?

If that gives you some results, try doing a printf("0x%08x\n", ffData.dwFileAttributes); for every file it retrieves too, and then see the difference. As in, what does that variable hold when you get a directory as opposed to when you get a file?

Actually there are two directories in that folder, as in all folders. They are "." and ".."

Know why?

Is one the directory itself and the other is the parent directory? That's my understanding of it, but when I said there were no other directories I meant no other folders to look in.

I'm starting to get some progress. Like idelovski and IceDane said, i was not seeing anything since I was not calling FindNextFile(). Now I am able to get through a directory, but I am getting stuck on switching directories correctly. If I cannot figure something out tonight I will post my code.

FindFirstFile() first finds the '.' folder, then the '..' folder, then the first item in the folder.

This only happens if you use '*.*' filter

I'm starting to get some progress. Like idelovski and IceDane said, i was not seeing anything since I was not calling FindNextFile(). Now I am able to get through a directory, but I am getting stuck on switching directories correctly. If I cannot figure something out tonight I will post my code.

Send the folder in as a param. Each time the FindFirstFile() finds a folder call it again (recursive).

1) I am able to read files, and go through directories just fine now. However, it will only back up one directory, and does not move down to the next directory, and will just search the same directory over and over. (ie. starts at c:\\ then goes to aaaTEST directory, and inside aaaTEST there are two folders but it will never back out and go to the second folder, always backs up to aaaTEST then back to the first folder).

2) It does not catch the name of a file on the first time through a folder. It will catch it on the second time through. I have no idea why this happens.

As some have noted before, the best way is recursion. The other problem I see in the code is that you have multiple paths for the same code or situation. It's like, is it a directory, do this, but if it isn't, do this and then if it's a directroy... etc.
That means multiple code paths to maintain, which isn't always preferable.

I went back and did about half a dozen different approaches to scanning directories and files, and eventually came up with one that worked correctly. I set it up to display the current folder, and file the program is scanning. However, I noticed after making some test files and test folders I made an error. I set my test folders up like 'aaTESTFOLDER' (in c:\ ) so it would be the first folder accessed just for the ease of testing. Then I took an image file and named it 'aaaa.jpg' (also in c:\ ). When I go to My Computer and open the explorer, folders are always placed on top in alphabetical order, then files, also in order. But I noticed FindNextFile() gets the first file OR directory according to alphabetical order. So my aaaa.jpg file was found first, skipped, then aaTESTFOLDER was found, accessed, and it become recursive from that point on. Here is where the problem comes in. I based my assumption of directory changing/file reading on the fact that the computer sees exactly what i see (directories first, then files, obviously not the case). I wanted to expand the file search/scan to virtually the same as the feature "Search..." under the start menu so it would search an entire drive. I am lost on how to approach changing back to a parent directory, then down to the next directory (so it does not back up, then go back to the same directory it was just in).

Here is my search that I got working up until it hits a dead end (does not go back to the parent).

No, no, no. You didn't grasp the concept people were telling you about, I think.
However, I think code may say more than a thousand words, so I'll post my code for file scanning. Your objective is to read the code and see how it is structured and how its layout is working. Not copy n' paste.
Note that the copy will not compiler as-is and is always a draft; some things may not be what they should be and the code may have some bad practices left.