how to get only folder count in a directory
-
wrote on 16 Nov 2017, 10:14 last edited by
hi
i want get only count of folders in a directory not files -
Hi
there is alsoQDirIterator it("/etc", QDirIterator::Subdirectories); while (it.hasNext()) { qDebug() << it.next();
-
Hi
there is alsoQDirIterator it("/etc", QDirIterator::Subdirectories); while (it.hasNext()) { qDebug() << it.next();
wrote on 16 Nov 2017, 10:28 last edited by@mrjj said in how to get only folder count in a directory:
QDirIterator::Subdirectories
i want get only folder count in current directory not sub directories
-
@mrjj said in how to get only folder count in a directory:
QDirIterator::Subdirectories
i want get only folder count in current directory not sub directories
-
@mrjj said in how to get only folder count in a directory:
QDirIterator::Subdirectories
i want get only folder count in current directory not sub directories
@ARASHz4
Ok, so only first level of subdirectories.
Try with QDir::entryList() then as @JNBarchan suggests. -
@mrjj said in how to get only folder count in a directory:
QDirIterator::Subdirectories
i want get only folder count in current directory not sub directories
wrote on 16 Nov 2017, 10:45 last edited by@ARASHz4
Or, if you do want to useQDirIterator
, you need to use theQDirIterator::QDirIterator(const QString &path, QDir::Filters filters, IteratorFlags flags = NoIteratorFlags)
overload and setfilters
(notflags
) as per what I wrote earlier. -
@ARASHz4
Or, if you do want to useQDirIterator
, you need to use theQDirIterator::QDirIterator(const QString &path, QDir::Filters filters, IteratorFlags flags = NoIteratorFlags)
overload and setfilters
(notflags
) as per what I wrote earlier.wrote on 16 Nov 2017, 10:58 last edited by@JNBarchan i try :
QDir(FolderAddress).entryList(QDir::AllDirs | QDir::NoDotAndDotDot).count()
this return folder count in a directory
thank you
-
@JNBarchan i try :
QDir(FolderAddress).entryList(QDir::AllDirs | QDir::NoDotAndDotDot).count()
this return folder count in a directory
thank you
wrote on 16 Nov 2017, 11:02 last edited by@ARASHz4
That's right, and just what I suggested :(Note that this simplest code builds a list of all directories in memory and then counts them. I think that's fine for a reasonable number of sub-directories. However, if you have like 1,000+ that's where
QDirIterator
approach would be more efficient (memory-wise) by not building a list, but requires a touch more lines of code, if you ever feel like it. -
@ARASHz4
That's right, and just what I suggested :(Note that this simplest code builds a list of all directories in memory and then counts them. I think that's fine for a reasonable number of sub-directories. However, if you have like 1,000+ that's where
QDirIterator
approach would be more efficient (memory-wise) by not building a list, but requires a touch more lines of code, if you ever feel like it.wrote on 16 Nov 2017, 11:14 last edited by@JNBarchan
You can call http://doc.qt.io/qt-5/qdiriterator.html#fileInfo and http://doc.qt.io/qt-5/qfileinfo.html#isDir to only count directories.
-
@JNBarchan
You can call http://doc.qt.io/qt-5/qdiriterator.html#fileInfo and http://doc.qt.io/qt-5/qfileinfo.html#isDir to only count directories.
wrote on 16 Nov 2017, 11:19 last edited by@t3685
But that is likely to be "inefficient", since you have to visit every entry (files as well as directories) in your code and then test whether it's a directory. That's the whole point of using theQDir::Filters
approach, which (presumably) allows the Qt code to use whatever native function might be available to only pick out directories in the first place.... -
@ARASHz4
That's right, and just what I suggested :(Note that this simplest code builds a list of all directories in memory and then counts them. I think that's fine for a reasonable number of sub-directories. However, if you have like 1,000+ that's where
QDirIterator
approach would be more efficient (memory-wise) by not building a list, but requires a touch more lines of code, if you ever feel like it.wrote on 16 Nov 2017, 11:22 last edited by@JNBarchan can you show me a example with QDirIterator for get folder count
-
@t3685
But that is likely to be "inefficient", since you have to visit every entry (files as well as directories) in your code and then test whether it's a directory. That's the whole point of using theQDir::Filters
approach, which (presumably) allows the Qt code to use whatever native function might be available to only pick out directories in the first place....wrote on 16 Nov 2017, 11:35 last edited by@JNBarchan I think this code no builds a list of all directories in memory
QDir folder (FolderAddress); folder.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot); int folderCount = folder.count();
-
@JNBarchan I think this code no builds a list of all directories in memory
QDir folder (FolderAddress); folder.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot); int folderCount = folder.count();
wrote on 16 Nov 2017, 11:40 last edited by JonB@ARASHz4 said in how to get only folder count in a directory:
@JNBarchan I think this code no builds a list of all directories in memory
QDir folder (FolderAddress); folder.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot); int folderCount = folder.count();
I believe that way does build a complete list, internally, and then evaluates the
count()
on the list. Need to useQDirIterator
instead ofQDir
if want be sure no list built.
1/15