Sign in to follow this  
Ajare

Directory Listing

Recommended Posts

I'm trying to find a way to get a directory listing in C/C++. I can't find anything, even in MSDN, but it must be possible. Basically, something that returns a list of pointers to file handles, or something. TIA.

Share this post


Link to post
Share on other sites
Ah i posted this a while back on a related thread, you might be interested.


#define WIN32_LEAN_AND_MEAN
//the define below is important you must have it
#define _WIN32_WINNT 0x0400

#include <windows.h>
#include <cstdlib>
#include <string>
#include <list>
#include <algorithm>
#include <iterator>
#include <iostream>

bool enumerate_dir(const std::string& dir, std::list<std::string>&);

int main(int argc, char *argv[]) {


if(argc == 1) {

std::cerr << argv[0] << " error usage: " << argv[0] << " <name-of-directory>\n";
return EXIT_FAILURE;
}

std::list<std::string> dir_list;

if(enumerate_dir(argv[1], dir_list))
std::copy(dir_list.begin(),
dir_list.end(),
std::ostream_iterator<std::string>(std::cout, ", "));
else
std::cerr << "No such Directory, please try again..";

return EXIT_SUCCESS;

}

bool enumerate_dir(const std::string& dir, std::list<std::string>& l) {

WIN32_FIND_DATA FindFileData;
HANDLE hFind;

hFind = FindFirstFileEx(dir.c_str(), FindExInfoStandard, &FindFileData,
FindExSearchNameMatch, 0, 0);

if(hFind == INVALID_HANDLE_VALUE)
return false;

FindNextFile(hFind, &FindFileData);

while(GetLastError() != ERROR_NO_MORE_FILES) {
FindNextFile(hFind, &FindFileData);
l.push_back(FindFileData.cFileName);
}

return true;
}



if you need something cross-platform you might wont to check out boost::filesystem

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