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. [Solved]How to list all files on linux correctly
Forum Updated to NodeBB v4.3 + New Features

[Solved]How to list all files on linux correctly

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.9k Views 1 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.
  • A Offline
    A Offline
    antonyprojr
    wrote on last edited by
    #1

    I want to get all files on linux, and i'm trying this code:

    @
    void MainWindow::countstart()
    {
    QFileInfoList drives = QDir::drives();
    foreach (QFileInfo drive, drives)
    {
    count(drive.absoluteFilePath());
    }
    }
    int total;
    void MainWindow::count(QString path)
    {
    QDir dir(path);
    QFileInfoList filelist = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
    foreach(QFileInfo fileinfo, filelist)
    {
    if (fileinfo.isDir())
    {
    count(fileinfo.absoluteFilePath());
    }
    if (fileinfo.isFile())
    {
    total++;
    }
    }
    }
    @

    but after 100.000.000 files listed it don't stop and the system has only 300.000 files,
    how can i list files correctly?
    Note: On Windows the code works correctly

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      Since you don't tell us what listing() is supposed to do, or how you have attempted to do it, we have no way to know what you are doing wrong.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        antonyprojr
        wrote on last edited by
        #3

        I posted the full code, what i'm doing wrong?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AcerExtensa
          wrote on last edited by
          #4

          Recursive function? - you are doing it wrong! :)

          You have forgot about symlinks, they can be recursive too. like symlink local in /usr pointing to ../ ;) or /usr/X11R6 linking back to /usr

          Also, int as an file counter is a bit to small, isn't it?

          And if you are doing an recursion, do it in the right way and don't forget about memory usage(it will increase speed too)

          working sample:
          @
          #include <QtCore/QCoreApplication>
          #include <QDebug>
          #include <QDir>
          #include <QFileInfoList>
          #include <QFileInfo>

          long long countFiles(const QDir & dir)
          {
          long long counter = 0;
          const QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
          foreach(const QFileInfo & info, list)
          {
          if(info.isFile() && !info.isSymLink())counter++;
          else if(info.isDir() && !info.isSymLink())counter += countFiles(info.absoluteFilePath());
          }
          return counter;
          }

          int main(int argc, char *argv[])
          {
          QCoreApplication a(argc, argv);
          QDir dir("/");
          qDebug() << "Files on system: " << countFiles(dir);
          return a.exec();
          }

          @

          God is Real unless explicitly declared as Integer.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            antonyprojr
            wrote on last edited by
            #5

            Thanks, it worked!

            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