Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do you check a file exits in another file ?
Forum Updated to NodeBB v4.3 + New Features

How do you check a file exits in another file ?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • QT_QT_QTQ Offline
    QT_QT_QTQ Offline
    QT_QT_QT
    wrote on last edited by
    #1

    I know you guys will have a lot of doubts regarding to the tile of this topic. Dont worry! I'll give a further explanation of my title. OK, lets start with this 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 a .txt file inside folder C. Right now, i want to check the .txt. file in 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 .txt file in folder A?

    #include "checkforfiles_form.h"
    #include "ui_checkforfiles_form.h"
    #include <QFileInfo>
    #include <qdebug.h>
    
    using namespace std;
    
    CheckForFiles_Form::CheckForFiles_Form(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::CheckForFiles_Form)
    {
        ui->setupUi(this);
    }
    
    CheckForFiles_Form::~CheckForFiles_Form()
    {
        delete ui;
    }
    
    void CheckForFiles_Form::on_pushButton_clicked()
    {
        QFileInfo fileInfo;
        fileInfo.setFile("C:\\Users\\USER\\Desktop\\folderA");
        qDebug()<<fileInfo.exists();
    }
    

    Above codes only will tell us folder A is really exist in that directory . But this is not i want , i want to search through the Folder A go inside the folder A, and check for which files in folder A containing .txt file?is it B,C or D? Some one here please give me some knowledge ,tips, reference or live example to proceed my task . Thank you

    the_T 1 Reply Last reply
    0
    • QT_QT_QTQ QT_QT_QT

      I know you guys will have a lot of doubts regarding to the tile of this topic. Dont worry! I'll give a further explanation of my title. OK, lets start with this 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 a .txt file inside folder C. Right now, i want to check the .txt. file in 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 .txt file in folder A?

      #include "checkforfiles_form.h"
      #include "ui_checkforfiles_form.h"
      #include <QFileInfo>
      #include <qdebug.h>
      
      using namespace std;
      
      CheckForFiles_Form::CheckForFiles_Form(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::CheckForFiles_Form)
      {
          ui->setupUi(this);
      }
      
      CheckForFiles_Form::~CheckForFiles_Form()
      {
          delete ui;
      }
      
      void CheckForFiles_Form::on_pushButton_clicked()
      {
          QFileInfo fileInfo;
          fileInfo.setFile("C:\\Users\\USER\\Desktop\\folderA");
          qDebug()<<fileInfo.exists();
      }
      

      Above codes only will tell us folder A is really exist in that directory . But this is not i want , i want to search through the Folder A go inside the folder A, and check for which files in folder A containing .txt file?is it B,C or D? Some one here please give me some knowledge ,tips, reference or live example to proceed my task . Thank you

      the_T Offline
      the_T Offline
      the_
      wrote on last edited by
      #2

      @QT_QT_QT

      QStringList QDir::entryList(Filters filters = NoFilter, sort = NoSort)
      

      may be what you are looking for.

      For example (if i understood your scenario right)

      QDir folderA("C:\\Users\\USER\\Desktop\\folderA");
      QStringList foldersinA = folderA.entryList(QDir::Dirs);
      foreach(QString folder,foldersinA) {
        QDir f(folder);
        QStringList txtlist = f.entryList(QStringList() << "*.txt",QDir::Files);
        //do something with the filenames in txtlist
      }
      
      

      -- No support in PM --

      1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Since you want to check several folder, you can use QDirIterator in addition to what @the_ suggested.

        On a side note, since your a using Qt, you should use the unix notation for path (i.e. "C:/Users/USER/Desktop/") that will avoid you headaches if your forget to escape your backslashes.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • QT_QT_QTQ Offline
          QT_QT_QTQ Offline
          QT_QT_QT
          wrote on last edited by
          #4

          Dear All, this is the solution for this specific problem . Thank you

          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);
          }
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Why not use QDirIterator ? You'd only have to do the file listing and check.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved