Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can QDir filter by filename length ?
Qt 6.11 is out! See what's new in the release blog

Can QDir filter by filename length ?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 93 Views 1 Watching
  • 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote last edited by
    #1

    Custom filter first.

    void QDir::setFilter(QDir::Filters filters)

    https://github.com/sonichy

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SabarishR
      wrote last edited by
      #2

      No, QDir itself can’t filter files by filename length directly.

      QDir only 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 with QFileInfo.

      So in short: not built-in, but easy to implement with a simple loop 👍

      SabarishR

      1 Reply Last reply
      2
      • S Offline
        S Offline
        SimonSchroeder
        wrote last edited by
        #3

        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.

        1 Reply Last reply
        2
        • sonichyS sonichy has marked this topic as solved

        • Login

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