Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Search for folder when the directory is not known
Qt 6.12 LTS is out! See what's new in the release blog

Search for folder when the directory is not known

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 305 Views
  • 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.
  • Y Offline
    Y Offline
    Ylvy
    wrote on last edited by Ylvy
    #1

    I'm searching for a specific folder when its directory is unknown, the folder contains a _ in its name, I wonder if its possible to filter the folders.
    In the sample below its taking so long to find the folder.

    void searchFolderInAllDrives(const QString &folderName)
    {
        QElapsedTimer timer;
        timer.start();
    
        QList<QStorageInfo> drives = QStorageInfo::mountedVolumes();
    
        QString folderPath;
        for (const QStorageInfo &drive : drives)
        {
            QDirIterator it(drive.rootPath(), QStringList() << folderName, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
    
            if (it.hasNext()) 
            {
                folderPath = it.next();
                qDebug() << "Found folder:" << folderPath;
                break;
            }      
        }
    
        qint64 elapsedTime = timer.elapsed();
        qDebug() << "Time taken to find the folder:" << elapsedTime << "ms";
    }
    
    
    
    
    int main(int argc, char *argv[])
    {
        searchFolderInAllDrives("folder_name");
        return 0;
    }
    
    JonBJ 1 Reply Last reply
    0
    • Y Ylvy

      I'm searching for a specific folder when its directory is unknown, the folder contains a _ in its name, I wonder if its possible to filter the folders.
      In the sample below its taking so long to find the folder.

      void searchFolderInAllDrives(const QString &folderName)
      {
          QElapsedTimer timer;
          timer.start();
      
          QList<QStorageInfo> drives = QStorageInfo::mountedVolumes();
      
          QString folderPath;
          for (const QStorageInfo &drive : drives)
          {
              QDirIterator it(drive.rootPath(), QStringList() << folderName, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
      
              if (it.hasNext()) 
              {
                  folderPath = it.next();
                  qDebug() << "Found folder:" << folderPath;
                  break;
              }      
          }
      
          qint64 elapsedTime = timer.elapsed();
          qDebug() << "Time taken to find the folder:" << elapsedTime << "ms";
      }
      
      
      
      
      int main(int argc, char *argv[])
      {
          searchFolderInAllDrives("folder_name");
          return 0;
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Ylvy
      What would you expect to be any faster than this? It has to search all directory entries recursively on all mounted volumes. That takes a while :) Linux filing systems don't have a magical call for finding things other than you looking at the filename retrieved, and these days I don't think Windows has a faster native call.

      This is also why OSs offer features like Windows "indexing" or Linux locate, so you can look up previously cached answers quickly!

      1 Reply Last reply
      2

      • Login

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