Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello.

Can anyone help me understand how findfirstfile and findnextfile work?

I just don't get it and am unable to link to a page that explains it from scratch.
Posted
Updated 7-May-11 21:49pm
v2

 
Share this answer
 
You call FindFirstFile to set up a file specification to look for.
You can then call FindNextFile repeatedly (normally in a loop) to iterate through each file that matches the specification.

I don't think you googled too hard: your question subject led me straight to MSDN and an example:
http://msdn.microsoft.com/en-us/library/aa365200(v=vs.85).aspx[^]
 
Share this answer
 
Comments
Hans Dietrich 8-May-11 3:54am    
I love one of the user comments on that MSDN page: "This program doesn't work on Windows 7 machine, when trying to get information from a UNIX machine."
Member 12630147 4-Aug-16 9:33am    
how to list all csv files in a combobox (in mfc application)
I understand your frustration - some ways of doing things are not untuitive to all ...

Ok - the fundamental problem with a directory listing is it's variable length, and it can be enormous, the way findfirst/next works is

FindFirst() starts a new query off, given a search string, that query is represented by the handle you get returned, then calling FindNext() with the handle you get, takes you to the next item on the result list (and under the hood, the OS 'remembers' you're moved down the list)

Subsequent FindNext()s on that handle continue to traverse the list until it's exhausted (and again, under the hood, the OS moves you along the list each time)

Make sure you call FindClose(); that essentially tells the OS you've finished with the list and it can let it go - you don't have to iterate to the end before you do a close (say you're looking for a particular filesize, you find it, you can terminate the list early with a FindClose()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900