65.9K
CodeProject 正在变化。 阅读更多。
Home

SADirRead - 目录和文件列表类

2001年1月24日

CPOL
viewsIcon

294037

downloadIcon

3629

扫描文件夹中的子文件夹和文件。简单易用。

引言

CSADirRead 是一个类,它将生成包含在您选择的文件夹中的文件和文件夹列表。

用法

首先,声明一个 CSADirRead 对象

#include "SADirRead.h"

...

CSADirRead dr;

首先,我们构建要扫描的文件夹列表。

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);
}

现在对象知道要扫描哪些目录,告诉它扫描文件

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);
}

由于您可以在扫描文件之前访问和修改文件夹列表,因此可以手动将文件夹添加到(或从)文件夹列表中。 同样,您可以多次调用 GetDirs() 来构建文件夹列表,然后再扫描文件,如下所示

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

对结果进行排序

在构建文件列表之后,可以使用 CSADirRead::SortFiles 对文件进行排序。 CSADirRead 有三种内置排序方法 - 字母顺序、日期和大小 - 并且可以按升序或降序排序。

获取 c:\windows 中的子文件夹列表

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

现在,files 将包含 C:\Windows 的子文件夹列表(不包括文件,仅包含文件夹)

历史

  • 2002 年 1 月 20 日 - 更新下载
  • 2002 年 5 月 24 日 - 更新下载
  • 2003 年 3 月 3 日 - 新版本。
© . All rights reserved.