smashthewindow Posted August 23, 2012 Share Posted August 23, 2012 Hi, This isn't directly related to Leadwerks but I guess someone can help me out here. I'm looking for a function that returns files under a directory on Windows. (It doesn't have to be cross-platform.) So for example, an ideal example would be something like: vector<string> get_files_in_dir( string directory ); I'm fine with Win32 API functions but I'm trying to avoid using boost. Can anyone help me out here? Quote Blog & Portfolio Current project: moon.chase.star Link to comment Share on other sites More sharing options...
Rick Posted August 23, 2012 Share Posted August 23, 2012 HANDLE hFind; WIN32_FIND_DATA data; hFind = FindFirstFile("c:\\*.*", &data); if (hFind != INVALID_HANDLE_VALUE) { do { printf("%s\n", data.cFileName); } while (FindNextFile(hFind, &data)); FindClose(hFind); } Found from http://stackoverflow...-directory-in-c 2 Quote Link to comment Share on other sites More sharing options...
Roland Posted August 23, 2012 Share Posted August 23, 2012 typedef std::vector<std::string> StringArray; StringArray ListFiles( const std::string folder, const std::string& pattern ) { std::string root = folder; if( root[root.length()-1] != '\\' && root[root.length()-1] != '/' ) root += '/'; root += pattern; StringArray v; WIN32_FIND_DATA fd; HANDLE h = FindFirstFile( root.c_str(), &fd); if( h == INVALID_HANDLE_VALUE ) return v; if( fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY ) v.push_back(fd.cFileName); while ( FindNextFile(h, &fd) ) if( fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY ) v.push_back(fd.cFileName); FindClose( h ); return v; } StringArray ListSubfolders( const std::string folder ) { StringArray v; std::string root = folder; if( root[root.length()-1] != '\\' && root[root.length()-1] != '/' ) root += '/'; root += "*"; WIN32_FIND_DATA fd; HANDLE h = FindFirstFile( root.c_str(), &fd); if( h == INVALID_HANDLE_VALUE ) return v; if( fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && strcmp( fd.cFileName, "." ) && strcmp( fd.cFileName, ".." ) ) { v.push_back(fd.cFileName); } while ( FindNextFile(h, &fd) ) { if( fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && strcmp( fd.cFileName, "." ) && strcmp( fd.cFileName, ".." ) ) { v.push_back(fd.cFileName); } } FindClose( h ); return v; } 1 Quote AV MX Linux Link to comment Share on other sites More sharing options...
smashthewindow Posted August 23, 2012 Author Share Posted August 23, 2012 Both looks good, thanks Rick & Roland. Quote Blog & Portfolio Current project: moon.chase.star Link to comment Share on other sites More sharing options...
Pixel Perfect Posted August 23, 2012 Share Posted August 23, 2012 Or there is this taken from a little util I wrote: void loadOutputFileDirectoryFileNames(void) { DIR *dp; struct dirent *dirp; fileListData* pFileListDataItem; std::string fullFileName; std::string fileName; std::string fileExt; if((dp = opendir(appData.currentOutputDirectory.c_str())) == NULL) { MessageBoxA(0,"An error occured trying to open the output directory","ERROR",0); // errno return; } clearDirectoryFileList(); while ((dirp = readdir(dp)) != NULL) { fullFileName = dirp->d_name; // Check the file extension if(getFileNameComponents(fullFileName, fileName, fileExt)) { for(int i = 0; i < vecExtList.size(); i++) { if(fileExt == vecExtList[i]) { pFileListDataItem = new fileListData; pFileListDataItem->currentFileName = fileName; pFileListDataItem->currentFileExt = fileExt; pFileListDataItem->updated= false; vecFileList.push_back(pFileListDataItem); break; } } } } closedir(dp); return; } requires dirent.h form the C POSIX library It's doing more than you require but its another alternative. Sorry for the poor tabs ... this container always seens to screw them up. How come your's is perfect Roland 1 Quote Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++ Link to comment Share on other sites More sharing options...
Roland Posted August 23, 2012 Share Posted August 23, 2012 My tabs are also screwed up. But as I can't stand seeing that I go in and edit afterwards. Quote AV MX Linux Link to comment Share on other sites More sharing options...
Pixel Perfect Posted August 23, 2012 Share Posted August 23, 2012 ahha ... that explains it ... cheers Roland Quote Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++ Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.