Solved how to get only folder count in a directory
-
@ARASHz4
That's what myentryList
above code approach does (pretty sure). Why don't you try it and see that's what it returns, and does not recurse downward? -
@ARASHz4
Ok, so only first level of subdirectories.
Try with QDir::entryList() then as @JNBarchan suggests. -
@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. -
@JNBarchan i try :
QDir(FolderAddress).entryList(QDir::AllDirs | QDir::NoDotAndDotDot).count()
this return folder count in a directory
thank you
-
@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. -
@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.
-
@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.... -
@JNBarchan can you show me a example with QDirIterator for get 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();
-
-
@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.