Function to read all file names in a directory?

Started by
6 comments, last by Doggan 18 years, 5 months ago
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!
Advertisement
What programming language? What operating system?
My bad.

C or C++, WinXP
I would consider using boost::filesystem.
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.
Is it possible for you to use C# (or managed C++)?

It's extremely easy using .NET.
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);	}
------------------------------ BOOMZAPTry our latest game, Jewels of Cleopatra
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!

This topic is closed to new replies.

Advertisement