checking files in all sub-directories



  • Hey guys! Give you a scenario . You have a folder A, inside the folder you ll have another three folders, the name of these three folder inside folder A are B,C and D. Now, Folder B and D is a empty folder, which means the folders B and D dont have anything inside the folder, but for folder C ,yes, its not empty, there is an another empty file called folder E which located inside folder C. Right now, i want to check the folder E in which folder , want to check either is in B,C or D. Let me make my question simpler, how do i check which files have the folder E in folder A?
    FYI, i have already implement some code for this scenario . Here the codes:

    void CheckForFiles_Form ::recursiveAddDir(QDir d, bool recursive)
    {
    
        d.setSorting(QDir::Name);
        QDir::Filters df = QDir::Files | QDir::NoDotAndDotDot;
        if(recursive) df |= QDir::Dirs;
        QStringList qsl = d.entryList  (df,QDir::Name|QDir::DirsFirst);
        foreach (const QString &entry, qsl){
            QFileInfo fInfo(d,entry);
            if(fInfo.isDir()){
                QDir sd(fInfo.absoluteFilePath());
                recursiveAddDir(sd,true);
            }else{
                if(**fInfo.completeSuffix()== "txt"**)
                               qDebug() << fInfo.filePath();
    
    
    void CheckForFiles_Form::on_pushButton_clicked()
    {
        QDir r("C:/Users/USER/Desktop/FileA/");
        recursiveAddDir(r, true);
    }
    

    Above codes can only look for extension of a file , eg: ..mp3,..zip and *.txt . This is not what i want, what i wanted is to look for the name of the folder .not the extension. I tried modify the codes that i provided, what have i modified is instead of fInfo.completeSuffix()=="txt" , i changed to fInfo.completeBaseName()=="fileE". The modification that i made will give you no error when you compile and build, but, no result will display out when you try to execute this function . Some one here please guide me , i already done most of the implementation of this problem , right now i just need to do some modification in order to meet my requirement. thank you!


  • Qt Champions 2016

    Hi
    You should check with qDebug what files it really finds
    Also
    fInfo.completeBaseName() should be it.
    So QDebug all you compare.
    also try
    fInfo.completeBaseName().Contains("fileE");
    or
    http://doc.qt.io/qt-5/qstring.html#indexOf

    I am not sure == compares as you think.


  • Lifetime Qt Champion

    Hi,

    Since you are looking for a folder, why not filter your entry list ask only for the Dirs ? Then it's a simple check with QStringList::contains.



  • @SGaist

    Hi, thanks for replying. I dont really understand the comment that you posted, perhaps you can give an example, so that i can have a clear picture. if(recursive) {df |= QDir::Dirs;} , isn't this line to ask only for dir? thank you.



  •  QString root = "C:/Users/USER/Desktop/FileA/";
        QDirIterator it(root,QStringList() << "fileE",QDir::Files,QDirIterator::Subdirectories);
                while(it.hasNext())
        {
            qDebug() << it.next();
            qDebug() << it.fileName();
        }
    

    tried to list all the directories and perform the checking operation unto it. But , this only work with file extension. Initially, its was "*.txt" , but i changed to "fileE" , it will give no errors when you try to compile. However, it doesn't produce any result . I m really dont have clues to proceed anymore. All i want to list all subdirectories, perform a checking operation unto the sundiretories, and lastly return an absolute path for the result . Thank you



  • @QT_QT_QT
    Does there any file named fileE exist in any subfolders as you use QDir::Files as filter?

    QDirIterator(const QString &path, const QStringList &nameFilters, QDir::Filters filters = QDir::NoFilter, IteratorFlags flags = NoIteratorFlags) only finds any entries, that have exact matching if you put a string without wildcards into the QStringList. So if you do not have any fileE somewhere it will be empty.

    If you want to search for folders you should use QDir::Dirs for the filter parameter



  • Dear All, after hours of struggling . Finally i come out with a solution ! Feel free to take a look!
    Credit to @SGaist @mrjj @the_

    //This is the fliters
    QDir::Filters df = QDir::Dirs |QDir::NoDotAndDotDot;
        QDirIterator::IteratorFlag dff = QDirIterator::Subdirectories;
    
    
    
        QString root = "C:/Users/USER/Desktop/FileA/";
        QDirIterator it(root,df,dff);
         while(it.hasNext())
        {
            //performing checking operation
           //QString str = "fileE";
             QString str2 = it.next();
    
             if(str2.contains("fileE"))
             {
                qDebug()<<str2;
             }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.