Click here to Skip to main content
Click here to Skip to main content

SADirRead - directory and file list class

By , 2 Mar 2003
Rate this:
Please Sign up or sign in to vote.

Introduction

CSADirRead is a class that will generate a list of files and folders contained in a folder of your choice.

Usage

First, declare a CSADirRead object

#include <span class="code-string">"SADirRead.h"</span>

...

CSADirRead dr;

First, we build our list of folders to scan.

dr.ClearDirs();         // start clean
dr.GetDirs("c:\\temp", true); // get all folders under c:\temp

// dump the directory list to the debug trace window:

// get the dir array
CSADirRead::SADirVector &dirs = dr.Dirs();

// loop over it
for (CSADirRead::SADirVector::iterator dit = dirs.begin(); 
     dit!=dirs.end(); dit++)
{
    TRACE("%s\n", (*dit).m_sName);
}

Now that the object knows which directories to scan, tell it to scan for files:

dr.ClearFiles();        // start clean

dr.GetFiles("*.jpg");   // get all *.JPG files in c:\temp and below

// get the file array
CSADirRead::SAFileVector &files = dr.Files();   

// dump it...
for (CSADirRead::SAFileVector::iterator fit = files.begin(); 
     fit!=files.end(); fit++)
{
    TRACE("%s\n", (*fit).m_sName);
}

Because you can access and modify the folder list before scanning for files, you can manually add folders to (or remove folders from) the folder list. Likewise, you can call GetDirs() multiple times to build up the folder list before scanning for files, like this:

dr.ClearDirs();                   // start clean
dr.GetDirs("c:\\temp", true);     // get all folders under c:\temp, recursively
dr.GetDirs("c:\\windows", false); // look in c:\windows. but don't recurse 
                                  // to sub-folders

dr.ClearFiles();
dr.GetFiles("*.JPG"); // gets files from all of the above folders

Sorting the results

After building the file list, you can sort the files using CSADirRead::SortFiles. CSADirRead has three built-in sorting methods - alphabetic, date and size - and can sort in ascending or descending order.

Get a list of subfolders in c:\windows

CSADirRead dr;
dr.GetDirs("c:\\windows", false);
dr.GetFiles("*.*", false, true);
 
// get the file array
CSADirRead::SAFileVector &files = dr.Files();   

Now, files will have a list of subfolder of C:\Windows (no files included, just the folders)

History

  • 20 Jan 2002 - updated download
  • 24 May 2002 - updated download
  • 3 March 2003 - new revision.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Chris Losinger
Software Developer
United States United States
Chris Losinger is the president of Smaller Animals Software, Inc..

Comments and Discussions

 
Generalfile name from file handle ! Pinmemberbfadi24-Feb-03 7:42 
Questionhow to get path to my own executable? PinmemberJesse Evans4-Feb-03 11:34 
AnswerRe: how to get path to my own executable? PinmemberChris Losinger4-Feb-03 13:47 
GeneralRe: how to get path to my own executable? PinmemberJesse Evans5-Feb-03 7:00 
Generalfiles longer than 179 PinmemberHector T26-Nov-02 1:18 
GeneralRe: files longer than 179 PinmemberHector T26-Nov-02 2:01 
GeneralRe: files longer than 179 Pinmemberfunvill11-May-05 8:29 
GeneralUnhandled exception ...access violation PinmemberjimNLX18-Sep-02 9:07 
Chris,
 
I added your code, but can't get it to run. It did at one time run, but I don't know what has changed.
 
Any ideas?????
 
Here is the code:
 

UpdateData(TRUE);
CString m_path = _T("c:\\temp");
CString m_filter = _T("*.txt");
// this object will find and hold out list of files and folders
CSADirRead dr;

// look in subfolders ?
dr.Recurse() = true;
 
// get sub folders in this folder
dr.GetDirs(m_path);
 
/*
we could add more dirs to the list by doing this:
 
dr.GetDirs("d:\\folder\\");
*/
 
// dump the current directory list
CSADirRead::SADirVector &dirs = dr.Dirs();
 
TRACE("Dirs\n");
for (CSADirRead::SADirVector::const_iterator dit = dirs.begin(); dit!=dirs.end(); dit++)
{
TRACE("%s\n", (*dit).m_sName);
}
 
// dr has a list of dirs. find all files in those dirs that match the file filter
dr.GetFiles(m_filter);
 
// now dr has a list of files
 
// dump them
CSADirRead::SAFileVector &files = dr.Files();
 
TRACE("Pre-sort\n.%s Files in %s\n", m_filter, m_path);
for (CSADirRead::SAFileVector::const_iterator fit = files.begin(); fit!=files.end(); fit++)
{
TRACE("%s: %d bytes\n", (*fit).m_sName, (*fit).size);
}
 
/*
we could get more files here, by doing more "GetFiles" calls, like this:
 
dr.GetFiles("c*.txt");
*/
 

 
// dump them
TRACE("\nPost-sort\n. %s Files, sorted\n", m_filter);
 
for (fit = files.begin(); fit!=files.end(); fit++)
{
// CTime has ctors for both _FILETIME and time_t. yay.
CTime accessTime((*fit).time_access);
CTime createTime((*fit).time_create);
CTime writeTime((*fit).time_write);

TRACE("%s:\t\t\t%I64u bytes.\t\tAccess time: %s\t\tCreate time: %st\tWrite time: %s)\n",
(*fit).m_sName,
(*fit).size,
accessTime.Format("%b %d, %Y, %H:%M:%S"),
createTime.Format("%b %d, %Y, %H:%M:%S"),
writeTime.Format("%b %d, %Y, %H:%M:%S"));
}
 

GeneralRe: Unhandled exception ...access violation PinmemberChris Losinger18-Sep-02 9:09 
GeneralRe: Unhandled exception ...access violation PinmemberjimNLX18-Sep-02 9:39 
GeneralRe: Unhandled exception ...access violation PinmemberChris Losinger18-Sep-02 10:13 
GeneralSmall problem with find() PinmemberParesh Solanki11-Mar-02 0:16 
GeneralRe: Small problem with find() PinmemberChris Losinger11-Mar-02 1:49 
GeneralRe: Small problem with find() PinmemberParesh Solanki11-Mar-02 2:02 
General"Look In" Combo Box PinmemberGeorge Clarence23-Jan-02 19:10 
GeneralRe: "Look In" Combo Box PinmemberChris Losinger23-Jan-02 19:34 
GeneralRe: "Look In" Combo Box PinmemberGeorge Clarence24-Jan-02 2:38 
GeneralRe: "Look In" Combo Box PinmemberChris Losinger11-Mar-02 1:48 
QuestionWhat changes were made... PinmemberJ.G. Hattingh20-Jan-02 5:34 
AnswerRe: What changes were made... PinmemberChris Losinger20-Jan-02 5:34 
GeneralCToolBarCtrl Help!! PinmemberSilky Octopus19-Jan-02 5:05 
GeneralRe: CToolBarCtrl Help!! PinmemberSilky Octopus19-Jan-02 6:21 
GeneralGet File Date PinmemberPicasso18-Jan-02 5:57 
GeneralRe: Get File Date PinmemberChris Losinger18-Jan-02 5:58 
GeneralRe: Get File Date PinmemberPicasso19-Jan-02 4:33 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 2 Mar 2003
Article Copyright 2001 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid