Sign in to follow this  
LostSource

How to list all the .txt files in a folder?

Recommended Posts

LostSource    199
I'm currently working on a level editor in ascii, which is coming along good. But I've ran into a rather large problem. This is my problem: When the user clicks on the console where it says "Load Level", I have a menu pop up that will list all of the level .txt documents in the Level Editor folder. I was wondering how would I be able to generate a list of all the .txt files in a folder? For example: Say we have a folder named "Level Editor" with levelA.txt and levelB.txt and levelC.txt and the program doesn't know how many files are in the folder. For all it knows their could be 1 or 20 .txt files in the "Level Editor" folder. How would the program be able to provide a list of all the .txt files in the folder to the user/player? So on the screen it would show the user and the user would click on the file name. This part I can do, I just can't get the program to list all the .txt files in the folder. [rough draft idea] Please click on the level you would like to load ------------------------------------------------ levelA.txt levelB.txt levelC.txt [eof rough draft idea] Thank you ahead of time for all/any help!

Share this post


Link to post
Share on other sites
Illco    928
Which system? On Windows you can use FindFirstFile() and FindNextFile() -- see MSDN for how they work. On cross-platform I think the boost library will supply such things, but then again I don't know anything about boost so I'll pass on that one.

Greetz,

Illco

PS: made a link to MSDN.

Share this post


Link to post
Share on other sites
LostSource    199
Its on Windows, and I've tried looking in to using _finddata_t and _findfirst() but I didn't have to much luck... = / Maybe I should give it a second look...?

Edit: I'm using C++ and the compiler if it matters is C++.NET

Share this post


Link to post
Share on other sites
Toolmaker    967
I am assuming you're using Windows, but I could be totally wrong. In case you're using Windows, hang thight. In case you're not, you can either turn off the internets, or read along.

The following code is platform-specific for Windows:

#ifndef _WIN32_FILESEARCH_H_
#define _WIN32_FILESEARCH_H_

#include <string>
#include <windows.h>

using std::string;

class CFileSearch
{
public:
CFileSearch():m_hHandle(INVALID_HANDLE_VALUE) {}
CFileSearch(const string& strSearch)
{
Search(strSearch);
}

// Search
// Does a search and stores the result. Will return false on error
bool Search(const string& strSearch)
{
m_bHasNext = true;

m_hHandle = FindFirstFile(strSearch.c_str(), &m_hSearchResult);
if (m_hHandle == INVALID_HANDLE_VALUE)
m_bHasNext = false;

return m_bHasNext;
}

// GetNext
// Places the next return value in strResult. Will return false when there is no
// new result
bool GetNext(string &Result)
{
if (!m_bHasNext)
return (false);

Result = m_hSearchResult.cFileName;
m_bHasNext = FindNextFile(m_hHandle, &m_hSearchResult) ? true : false;

return (true);
}

private:
HANDLE m_hHandle;
WIN32_FIND_DATA m_hSearchResult;
bool m_bHasNext;
};

#endif



Using this code:

string path = "c:\\leveleditor\";
string result;

cout << "Current directory: " << path << endl;
CFileSearch search(path + "*.txt");
while (search.GetNext(result))
{
cout << result << endl;
}


This is some old code, so it MIGHT contain some errors, but you'll manage I hope. Otherwise you know where the find me(Hint: pm).

Toolmaker

Share this post


Link to post
Share on other sites
LostSource    199
Thank you everyone and thank you so much Toolmaker! The code still works and it works good! And it gave me a good reference to look at. Now I'm going to go and try to go code something that works like it, so I can learn how to do it and impleament it in my level editor. I also need to go do some research/reading on "FindFirstFile() and FindNextFile()".

Thank you so much again everyone!

Share this post


Link to post
Share on other sites
Deyja    920
The boost::filesystem library presents a much nicer interface than the win32 api or posix, and gives you portability to boot.

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