Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC
Article

SADirRead - directory and file list class

Rate me:
Please Sign up or sign in to vote.
4.88/5 (27 votes)
2 Mar 2003CPOL 283.5K   3.6K   80   86
Scans a folder for sub-folders and files. Simple and easy to use.

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 "SADirRead.h"

...

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)


Written By
Software Developer
United States United States
Chris Losinger was the president of Smaller Animals Software, Inc. (which no longer exists).

Comments and Discussions

 
GeneralExcellant Job Pin
Charlie Curtis29-Oct-08 11:03
Charlie Curtis29-Oct-08 11:03 
GeneralRe: Excellant Job Pin
Chris Losinger29-Oct-08 11:29
professionalChris Losinger29-Oct-08 11:29 
GeneralRe: Excellant Job Pin
Charlie Curtis29-Oct-08 11:37
Charlie Curtis29-Oct-08 11:37 
QuestionCan I have a C# version of this application over .NET 2.0 Pin
nitinr70822-Aug-07 0:31
nitinr70822-Aug-07 0:31 
QuestionBuggy Code? Pin
EScout2091-Feb-07 4:37
EScout2091-Feb-07 4:37 
AnswerRe: Buggy Code? Pin
Chris Losinger1-Feb-07 4:48
professionalChris Losinger1-Feb-07 4:48 
GeneralRe: Buggy Code? Pin
EScout2091-Feb-07 5:28
EScout2091-Feb-07 5:28 
GeneralRe: Buggy Code? Pin
Chris Losinger1-Feb-07 6:17
professionalChris Losinger1-Feb-07 6:17 
Generalworkin with simple Win32 app Pin
asif m@hmood11-Jan-07 2:41
asif m@hmood11-Jan-07 2:41 
GeneralRe: workin with simple Win32 app Pin
Chris Losinger11-Jan-07 2:50
professionalChris Losinger11-Jan-07 2:50 
GeneralRe: workin with simple Win32 app Pin
asif m@hmood12-Jan-07 2:03
asif m@hmood12-Jan-07 2:03 
Questionerror LNK2001: unresolved external symbol Pin
Mukund - - Kumar25-May-06 1:58
Mukund - - Kumar25-May-06 1:58 
AnswerRe: error LNK2001: unresolved external symbol Pin
Chris Losinger25-May-06 2:23
professionalChris Losinger25-May-06 2:23 
GeneralRe: error LNK2001: unresolved external symbol Pin
Mukund - - Kumar25-May-06 2:29
Mukund - - Kumar25-May-06 2:29 
GeneralRe: error LNK2001: unresolved external symbol Pin
Chris Losinger25-May-06 2:34
professionalChris Losinger25-May-06 2:34 
Questionunicode file name and directory name? Pin
dungbkhn20-May-06 22:42
dungbkhn20-May-06 22:42 
Generalformat file size Pin
Roger658-Mar-06 3:35
Roger658-Mar-06 3:35 
GeneralReturn relative path Pin
ovolok6-Apr-04 0:06
ovolok6-Apr-04 0:06 
GeneralRe: Return relative path Pin
Chris Losinger6-Apr-04 2:10
professionalChris Losinger6-Apr-04 2:10 
you'll have to figure out the relative path yourself. but, it's a fairly simple string parsing process. something like this:

To: c:\foo\file.txt
From: c:\foo\subdir\file2.txt

remove the parts of the path that match:

To: \file.txt
From: \subdir\file2.txt

then, for each folder name in From, add ".." to the start of To:

..\file.txt


Cleek | Losinger Designs | ClickPic | ThumbNailer
Generalproblems with integrating Pin
cryptic_silences5-Apr-04 18:22
cryptic_silences5-Apr-04 18:22 
GeneralRe: problems with integrating Pin
cryptic_silences5-Apr-04 20:08
cryptic_silences5-Apr-04 20:08 
GeneralRe: problems with integrating Pin
bart13220-Jan-06 1:13
bart13220-Jan-06 1:13 
GeneralNewbie: How to integrate Class Pin
caribuni15-Jan-04 5:28
caribuni15-Jan-04 5:28 
GeneralRe: Newbie: How to integrate Class Pin
Chris Losinger15-Jan-04 6:10
professionalChris Losinger15-Jan-04 6:10 
GeneralRe: Newbie: How to integrate Class Pin
caribuni15-Jan-04 6:30
caribuni15-Jan-04 6:30 

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.