如何扫描目录树?

时间:2013-11-09 16:28:12

标签: c++

我需要做一种扫描,我已完成第一部分,程序将扫描目录通知并将文件与其他目录分开,但我还需要扫描其他目录,看看我的代码:

void CompleteScan(const char root[], WCHAR *Extension,std::vector<FileStruct> &Return_Files,std::vector<WIN32_FIND_DATA> &Return_Directorys){
std::vector<FileStruct> files;
std::vector<WIN32_FIND_DATA> dir;

Scan(root, Extension, files, dir);

if(dir.size() > 0){
    for(int i = 0; i < dir.size(); i++){
        /*          Here is directory by directory scan         */
    }
}

命令Scan返回FileStruct的向量(目录和WIN32_FIND_DATA)和向导的向量,但是我需要扫描所有的Directorys,例如:

“C:\”程序扫描这个并返回“C:\”上的所有directorys,然后我扫描所有的directorys,但我不知道我要做什么才能使它扫描所有的directorys on “C:\”directorys,请查看此列表,例如:

  1. C:\ {OK}
  2. C:\ Files {OK}
  3. C:\ Files \ First File {Can not}
  4. C:\ Files \ First File \ Second File {Can not}

2 个答案:

答案 0 :(得分:0)

您必须为找到的每个目录递归调用CompleteScan函数。

答案 1 :(得分:0)

递归!如果我丢弃任何标点符号,请原谅;我没有Windows框来编译/测试。

/**
 * @brief Accumulates the files and directories under the given root directory
 * into the output vectors allfiles and alldirs that you provide.
**/
void CompleteScan(const char root[],
                  WCHAR *Extension,
                  std::vector<FileStruct> &allfiles,
                  std::vector<WIN32_FIND_DATA> &alldirs)
{
    std::vector<WIN32_FIND_DATA> subdirs;

    // Assuming your Scan function does what needs to be done with the allfiles
    // vector. If not, more code needed somewhere here to handle that.
    Scan(root, Extension, allfiles, subdirs);

    if(dir.size() > 0){
        for(int i = 0; i < dir.size(); i++){
            // Visit this directory
            alldirs.push_back(dir);
            // Visit all descendant directories
            CompleteScan(dir.cFileName, Extension, allfiles, alldirs);
        }
    }
}