Sign in to follow this  
Rhaal

Opening files with a specified extension (Win32)

Recommended Posts

Basically what I need to do is search through all files in a specified folder, that are of a specific extension, open them and store their paths in memory. The only part I don't know how to go about implementing is the locating of files. I've looked through MSDN's file handling stuff and can't figure it out :'( So if I have these files: d:\folder\aaaa.txt d:\folder\bbbb.bmp d:\folder\cccc.ini d:\folder\dddd.txt d:\folder\eeee.txt I need to open each that has .txt and store it's path in memory. What API am I looking at to search for files based on extension and get a handle or at least a path from them? This is for Windows. THANKS!

Share this post


Link to post
Share on other sites
If you must use Win32, then the function you want is:
FindFirstFile.

The example at that page should provide what is needed so you can execute and store each path. Loop through all the files with FindNextFile, and don't forget to close with FindClose.

Share this post


Link to post
Share on other sites
I've come across another problem. I need to be able to tell how many files are in a specified directory so that I can iterate through properly. I'm thinking GetFileAttributesByHandle (http://msdn.microsoft.com/library/en-us/fileio/fs/getfileattributesbyhandle.asp?frame=true) to determine if a file is a directory, but how do I get a count out of that (how many files are in said directory)?

Edit: Nope, requires windows longhorn. ARG.

Share this post


Link to post
Share on other sites
Do you have to know the amount before searching?
You can iterate with: while (FindNextFile(hFind, &FindFileData) != 0)
If you want to count, you can count in that loop.

The example at Listing the files in a directory is not sufficient?

-edit-
Requirements
Client Requires Windows "Longhorn", Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.

It seems like 95+ works.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this