Sign in to follow this  

Function to read all file names in a directory?

This topic is 4416 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have never had the need to do this before, so I'm not too sure where to start. What is the basic method for a file/folder manipulation program? Essentially, I want to sort all of my media into a database. To do this, I need to get the names of all of the folders and files. This is my plan: I have a folder filled with NES (.nes) roms. I need a simple program that goes to a directory I tell it to, and reads all of the filenames in that folder, placing them into a text file. How do I go about reading all the file names in a folder? (Once i get the names, I can obviously make my own algorithm to parse base on extension). Thanks!

Share this post


Link to post
Share on other sites
You know it's a heck of a lot faster to do this in a shell scripting language, including MS-DOS batch processing language. dir *.nes > nesfiles.txt to get only the .nes files; vary the wildcards for other formulations.

For straight Win32, see Listing the Files in a Directory.

Otherwise, go with SiCrane's recommendation of boost::filesystem.

Share this post


Link to post
Share on other sites
If you'd rather not include a 200 MB dependency that can't deal with spaces in file-names, here's the native Win32 method.



WIN32_FIND_DATA FileData;
HANDLE hFile = FindFirstFile(Path.c_str(), & FileData);
bool FileOK = (hFile != INVALID_HANDLE_VALUE);
while (FileOK)
{
FileOK = (FindNextFile(hFile, &FileData) != 0);
}

Share this post


Link to post
Share on other sites
Thanks for the help! I think I'm on the right track now.

Quote:
Original post by Daniel Miller
Is it possible for you to use C# (or managed C++)?

It's extremely easy using .NET.


That is an option. I've done some searching and I think I have some possible links. Thanks!

Share this post


Link to post
Share on other sites

This topic is 4416 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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