Can QDir filter by filename length ?
-
No,
QDiritself can’t filter files by filename length directly.QDironly supports basic name filters (like wildcards), sorting, and attribute-based filters (files, dirs, hidden, etc.). It doesn’t provide filtering based on properties like string length.The usual way to handle this is to get the file list first and then filter it manually. For example:
QDir dir("your/path"); QStringList files = dir.entryList(QDir::Files); QStringList filtered; for (const QString &file : files) { if (file.length() == 10) { // or whatever length you need filtered.append(file); } }If you need more file details, you can also use
entryInfoList()and work withQFileInfo.So in short: not built-in, but easy to implement with a simple loop 👍
-
You could use
QDir::setNameFilters(https://doc.qt.io/qt-6/qdir.html#setNameFilters). The documentation mentions that you can use*and?wildcards. If you don't know this:?filters exactly one character. So, to filter for a specific length, just use that number of question marks as name filter. -
S sonichy has marked this topic as solved