If the function is not documented to set the last-error code, the value returned by this function is simply the most recent last-error code to have been set; some functions set the last-error code to 0 on success and others do not.

Since the documentation for FindNextFile() doesn't say anything one way or the other, relying on it to return ERROR_SUCCESS is bad program design. There is no requirement at all that the function set ERROR_SUCCESS on a successful call.

08-27-2009

Ducky

Ok, so if im only checking if return !=0, it enters the loop only once,
the second time it returns 0 like it didnt find anything else.

You need to do this:
Find first file.
If above failed, quit.
Start loop.
Find next file.
If above call failed,
--> Check if there are no more files.
--> If there are no more files, quit loop.
--> Do some error recovery and/or error print/whatever.
Do what you want with the file.
Loop.
Close file handle.

08-27-2009

Sebastiani

Quote:

Originally Posted by Ducky

Ok i found the solution!

You need to use

Code:

do{}while

statement.

Right, because FindFirstFile grabs the first file, if available. Just be sure to check that it succeeds first, obviously.

Here's a slightly more portable method (not standard, but most systems support the functions used). The general approach here is to encapsulate the traversal logic in a reusable interface that allows you to simply pass callback functions/objects to it that get invoked at key points in the traversing routine, namely before entering a directory, when processing a file, and after exiting the directory. Another feature is that it restores the current working directory to it's original state. It even works with individual file names, so you don't have to incorporate any special logic to detect what type of file-system object you're dealing with.