Search for folder when the directory is not known
-
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; } -
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; }@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!