Function to read all file names in a directory?
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!
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.
For straight Win32, see Listing the Files in a Directory.
Otherwise, go with SiCrane's recommendation of boost::filesystem.
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); }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement