Results 1 to 3 of 3

Thread: How to exclude a specific file extension from QDir::entryList

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    677
    Thanks
    55
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to exclude a specific file extension from QDir::entryList

    Hi community,
    I know how to specify the file extensions that I would like to be considered when I call the QDir::entryList method:

    Qt Code:
    1. QDir dir(path);
    2. QStringList files = dir.entryList(QStringList() << "*.ext1" << "*.ext2", QDir::Files | QDir::NoDot | QDir::NoDotAndDotDot);
    To copy to clipboard, switch view to plain text mode 

    In this case the files having extensions ext1 and ext2 only are considered. But how to exclude a specific extension?
    For example I would like to considere ALL the extensions except a specific one ( in my case *.conf )

    Thx
    Franco Amato

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    487
    Thanks
    11
    Thanked 71 Times in 69 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to exclude a specific file extension from QDir::entryList

    Hi, I'm not sure if entryList() can do that. But you can simply get all files, then iterate over the stringlist and remove the matching files afterwards.

    Ginsengelf

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,134
    Thanks
    293
    Thanked 850 Times in 837 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to exclude a specific file extension from QDir::entryList

    Something like this:

    Qt Code:
    1. // Note that since QFileInfo::suffix() returns the extension -without- a leading ".", the code allows the
    2. // excluded extensions list to contain either "ext" or ".ext" and will check for both.
    3. //
    4. // This will also include files with no extensions, so if that is not desired, check the suffix for isEmpty()
    5. // and discard them too.
    6. //
    7. // Usage:
    8. //
    9. // QStringList noBinaryList = excludedEntryList( path, QStringList() << ".exe" << "dll", QDir::Files | QDir::NoDot | QDir::NoDotAndDotDot );
    10.  
    11. // Untested code! Might contain bugs
    12.  
    13. QStringList excludedEntryList( const QString & path, const QStringList & excludedExts, QDir::Filters filters )
    14. {
    15. QStringList filteredEntries;
    16. QDir dir( path );
    17. QFileInfoList unfilteredInfos = dir.entryInfoList( filters );
    18. for ( QFileInfo info : unfilteredInfos )
    19. {
    20. QString ext = info.suffix();
    21. if ( !excludedExts.contains( ext ) && !excludedExts.contains( "." + ext ) )
    22. filteredEntries.push_back( info.fullPath() );
    23. }
    24. return filteredEntries;
    25. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. The following user says thank you to d_stranz for this useful post:

    franco.amato (14th March 2022)

Similar Threads

  1. qmake exclude specific file in SOURCES
    By Scope in forum Qt Programming
    Replies: 7
    Last Post: 23rd July 2016, 22:08
  2. QDir::entryList() get absolute path
    By Aji Enrico in forum Qt Programming
    Replies: 3
    Last Post: 23rd April 2011, 04:26
  3. QDir - Entrylist, sort
    By bismitapadhy in forum Qt Programming
    Replies: 5
    Last Post: 28th January 2010, 06:27
  4. QDir::entryList() on linux
    By JeanC in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2008, 13:46
  5. qdir and entrylist
    By mattia in forum Newbie
    Replies: 1
    Last Post: 28th November 2007, 10:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.