1 year ago

#118152

test-img

Anshu

I want to folders parsing only at one level, take folder name and use it to construct path don't want to go in all subdirectory recursively

Following code I have implemented : I want to search enlist 2 folder present under directory and search for ref .txt file and build path . Current code is working , But it's searching every subdirectory present in 2 folder inside parent directory. It's delaying the process.

Eg: There are multiple folder/Sub directory present inside A and B both We don't want to traverse inside those .

folder structure is just for reference (but It's on linux) \A\ref.txt

\B\ref.txt

I want to get A and B only ( 1 level directory parse, make path and read ref.txt)

void getDir(std::string directory)
{
    std::string dirToOpen = path + directory;
    DIR * dir = opendir(dirToOpen.c_str());
    path = dirToOpen + "/";
    if(NULL == dir)
        return;
    struct dirent * entity = readdir(dir);
    while(entity != NULL)
    {
        getEntity(entity);
        entity = readdir(dir);
    }
    path.resize(path.length() - 1 - directory.length());
    closedir(dir);
}

void getEntity(struct dirent* entity)
{
    if (NULL == entity)
        return;
    if(entity->d_type == DT_DIR)
    {
        if(entity->d_name[0] == '.')
        {
            return;
        }
        getDir(std::string(entity->d_name));
        return;
    }
    if(entity->d_type == DT_REG)
    {
        buildPath(std::string(entity->d_name));
        return;
    }
}

void buildPath(std::string file)
{
    if (file == std::string("ref.txt")) 
    {
        std::cout << "file found " <<  std::endl;
        str::string Path_1 = path + file;
    }
}

c++

linux

directory

path

dirent.h

0 Answers

Your Answer

Accepted video resources