So for a little educational project, I’m trying to take the directory tree starting in a given directory and list all the files in it, which just happen to be found only in the bottom-level directories. Seems simple enough, but this is Windows, so it can’t be all that easy.
I’ve been trying to use FindFirstFile and FindNextFile, and was getting along rather well until I started finding directories with spaces in their long names. Those functions won’t find a directory with a space in its long name, and it doesn’t seem to work with the 8.3 name either.
I’m frustrated now, so I turn to the doper community. Is there an easy trick that I’m missing here?
Well, it’s definitely not the space. Funny thing is, the code I use to get the folder name corresponding to the artist is identical except for a few names (told you this was an early version).
Well, the first thing I see is you’re calling FindNextFile immediately after FindFirstFile, skipping the first two files. (How many files are in the Ace of Base folder?)
Typically you’d use the functions like so:
if ((handle = FindFirstFile(path, &data)) != INVALID_HANDLE_VALUE) {
do {
// do something with data
} while (FindNextFile(handle, &data) != 0);
FindClose(handle);
}
You are NOT guaranteed that “.” and “…” are always the first names returned. The correct procedure is to go through all of them, testing for “.” and “…” explicitly.
I just realized my mistake about that strncpy code :smack:
They’re not exactly the same, because my strcpy statement will copy the final NULL character. Still, it’s always a good idea to take the destination buffer size into account.