Click here to Skip to main content
16,010,360 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: iostream Pin
Nick Parker13-Dec-02 8:19
protectorNick Parker13-Dec-02 8:19 
GeneralRe: iostream Pin
will138313-Dec-02 8:29
will138313-Dec-02 8:29 
GeneralSearches Pin
will138313-Dec-02 7:49
will138313-Dec-02 7:49 
GeneralRe: Searches Pin
Alvaro Mendez13-Dec-02 8:15
Alvaro Mendez13-Dec-02 8:15 
GeneralRe: Searches Pin
will138313-Dec-02 8:27
will138313-Dec-02 8:27 
GeneralRe: Searches Pin
Alvaro Mendez13-Dec-02 9:16
Alvaro Mendez13-Dec-02 9:16 
GeneralRe: Searches Pin
will138313-Dec-02 9:41
will138313-Dec-02 9:41 
GeneralRe: Searches Pin
Todd Smith14-Dec-02 17:34
Todd Smith14-Dec-02 17:34 
typedef std::list<std::string> FileList;
typedef FileList::iterator FileListIterator;

struct SortByDate : public std::binary_function<std::string, std::string, bool>
{
public:
	SortByDate(const char* dir) : m_dir(dir) {}

	bool operator()(const std::string& lhs, const std::string& rhs)
	{
		struct stat st = {0};
		
		std::string filename = m_dir;
		filename += "\\";
		filename += lhs;

		if (stat(filename.c_str(), &st) == -1)
		{
			return true;
		}

		int lhs_ctime = st.st_ctime;

		filename = m_dir;
		filename += "\\";
		filename += rhs;

		if (stat(filename.c_str(), &st) == -1)
		{
			return false;
		}

		int rhs_ctime = st.st_ctime;

		return lhs_ctime < rhs_ctime;
	}
private:
	const char* m_dir;
};

// Returns a list of files in a directory sorted by date
// NOTE: does not recurse subdirectories
bool GetDirectoryList(const char* dir, FileList& fileList)
{
	DEBUGF("GetDirectoryList %s", dir);

	HANDLE hSearch;
	WIN32_FIND_DATA FindData;
	char tmpDir[MAX_PATH*2];

	lstrcpy(tmpDir, dir);
	lstrcat(tmpDir, "\\*.*");
	hSearch = FindFirstFile(tmpDir, &FindData);
	
	if (INVALID_HANDLE_VALUE == hSearch)
	{
		return false;
	}
	
	do 
	{
		if (!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
		{
			// Insert files sorted by date
			fileList.insert(std::upper_bound(fileList.begin(),
							 fileList.end(),
							 FindData.cFileName,
							 SortByDate(dir)), 
							 FindData.cFileName);
		}
	} 
	while (FindNextFile(hSearch, &FindData));

	FindClose(hSearch);
	return true;
}


Here's a simple example. You can exclude the SortByDate function if you like. I should have made it more STL like but didn't have time.

Todd Smith
GeneralLast directory visited Pin
doctorpi13-Dec-02 6:30
doctorpi13-Dec-02 6:30 
GeneralRe: Last directory visited Pin
Gary Kirkham13-Dec-02 6:55
Gary Kirkham13-Dec-02 6:55 
GeneralRe: Last directory visited Pin
Ravi Bhavnani13-Dec-02 9:38
professionalRavi Bhavnani13-Dec-02 9:38 
GeneralRe: Last directory visited Pin
Gary Kirkham13-Dec-02 10:29
Gary Kirkham13-Dec-02 10:29 
GeneralRe: Last directory visited Pin
Ravi Bhavnani13-Dec-02 11:30
professionalRavi Bhavnani13-Dec-02 11:30 
GeneralWindows Forms Open File Dialog Pin
KBL13-Dec-02 6:08
KBL13-Dec-02 6:08 
GeneralMFC vs. win32 Pin
naradaji13-Dec-02 5:50
naradaji13-Dec-02 5:50 
GeneralRe: MFC vs. win32 Pin
Rickard Andersson2013-Dec-02 6:05
Rickard Andersson2013-Dec-02 6:05 
GeneralRe: MFC vs. win32 Pin
Nemanja Trifunovic13-Dec-02 6:17
Nemanja Trifunovic13-Dec-02 6:17 
GeneralRe: MFC vs. win32 Pin
Christian Graus13-Dec-02 8:40
protectorChristian Graus13-Dec-02 8:40 
GeneralRe: MFC vs. win32 Pin
Rickard Andersson2013-Dec-02 10:30
Rickard Andersson2013-Dec-02 10:30 
GeneralRe: MFC vs. win32 Pin
Alvaro Mendez13-Dec-02 8:26
Alvaro Mendez13-Dec-02 8:26 
GeneralRe: MFC vs. win32 Pin
will138313-Dec-02 8:36
will138313-Dec-02 8:36 
GeneralRe: MFC vs. win32 Pin
Christian Graus13-Dec-02 8:42
protectorChristian Graus13-Dec-02 8:42 
GeneralRe: MFC vs. win32 Pin
Ravi Bhavnani13-Dec-02 9:41
professionalRavi Bhavnani13-Dec-02 9:41 
GeneralRe: MFC vs. win32 Pin
naradaji15-Dec-02 21:59
naradaji15-Dec-02 21:59 
GeneralRe: MFC vs. win32 Pin
naradaji17-Dec-02 4:07
naradaji17-Dec-02 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.