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. Quickest way to search a directory for files with a specified extension
Forum Updated to NodeBB v4.3 + New Features

Quickest way to search a directory for files with a specified extension

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 46.2k 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.
  • P Offline
    P Offline
    phamtv
    wrote on last edited by
    #1

    What would be the quickest way to search a directory for files with a specified extension (ie. ¨.txt¨)? Any help appreciated! This should include any subdirectories as well.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      RazrFalcon
      wrote on last edited by
      #2

      @#include <QtDebug>

      QDirIterator dirIt("/folder_path",QDirIterator::Subdirectories);
      while (dirIt.hasNext()) {
      dirIt.next();
      if (QFileInfo(dirIt.filePath()).isFile())
      if (QFileInfo(dirIt.filePath()).suffix() == "txt")
      qDebug()<<dirIt.filePath();
      }@

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cincirin
        wrote on last edited by
        #3

        ... or more simply "QDir::entryList":http://doc.qt.nokia.com/stable/qdir.html#entryList

        In your case :

        @
        QStringList nameFilter("*.txt");
        QDir directory(your_path);
        QStringList txtFilesAndDirectories = directory.entryList(nameFilter);
        @

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RazrFalcon
          wrote on last edited by
          #4

          [quote author="cincirin" date="1319612618"]... or more simply[/quote]
          but not recursive

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cincirin
            wrote on last edited by
            #5

            [quote author="RazrFalcon" date="1319612980"]but not recursive[/quote]

            Truth in, but @phamtv not said he wants recursive.
            I understand he wants all subdirectories from a given directory. :-)

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              You can easily make this a recursive method. Use "QDir::entryInfoList() ":http://doc.qt.nokia.com/4.7/qdir.html#entryInfoList, you get the info whether an entry is a directory from [[Doc:QFileInfo]].

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                I would make a recursive method that combines cincirin's method of listing the actual files with a recursive approach to get the subdirectories of the current directory, and do the same for each of those subdirectories.

                One thing about recursive methods like these:
                I foudn that it is convenient to wrap them in a non-recursive method. That method is then the API, the recursive one is an implementation detail that needs to be private. The reason is that often the method signature that you need for the recursive function is not optimal for the API of your class. It exposes too much internals of the recursive nature of the function. Though, I guess, you can do without in this case.

                1 Reply Last reply
                1

                • Login

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