Results 1 to 4 of 4

Thread: glob like fuction to list all files in subfolders that match wildcard.

  1. #1
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default glob like fuction to list all files in subfolders that match wildcard.

    Basically want something similar to the python glob command.

    glob("*/abc*.dat")

    would search the first level of subfolders and give me string list of file paths for all files (from cwd) starting with "abc" ending with ".dat"

    Is there a quick similar QT function?
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: glob like fuction to list all files in subfolders that match wildcard.

    Check out QDir, specifically QDir::entryList or QDir::entryInfoList, QDir::setNameFilters depending on your needs.

  3. #3
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: glob like fuction to list all files in subfolders that match wildcard.

    I tried that but unless I am doing something wrong, I can't get it to list files in subfolders

    If my folder structure is:
    folder1/abc1.txt
    folder1/xyz1.txt
    folder1/abc2.txt
    folder2/abc3.txt
    folder2/xyz4.txt
    folder3/abc5.txt


    then if I give "*/abc*"
    I'm looking for an output of
    folder1/abc1.txt
    folder1/abc2.txt
    folder2/abc3.txt
    folder3/abc5.txt


    I tried entrylist but it just lets me filter on whats in the current folder
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: glob like fuction to list all files in subfolders that match wildcard.

    Quote Originally Posted by enricong View Post
    I tried that but unless I am doing something wrong, I can't get it to list files in subfolders
    What filter(s) are you specifying for QDir::entryList or QDir::entryInfoList?

    The entryInfoList and entryInfo methods only operate on the directory path represented by the QDir variable instance. If you want to process sub-directories, you need to ask to receive directory entries *and* files and recurse into the sub-directories as well.

    I'd suggest something like this (I have not compiled or tested this, it's only from memory):

    Qt Code:
    1. void process_dir(const QString &path)
    2. {
    3. QDir dir(path);
    4. QStringList name_filters;
    5. name_filters << "abc*";
    6. QFileInfoList fil = dir.entryInfoList(name_filters, QDir::NoDotAndDotDot|QDir::AllDirs|QDir::Files);
    7. for (int i = 0; i < fil.size(); i++)
    8. {
    9. QFileInfo fi = fil.at(i);
    10. if (fi.isDir())
    11. process_dir(fi.absolutePath());
    12. if (fi.isFile())
    13. do_something_with_file(fi);
    14. }
    15. return;
    16. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 14th November 2013, 04:50
  2. command line parameters: wildcard problem
    By vonCZ in forum General Programming
    Replies: 2
    Last Post: 20th April 2013, 15:03
  3. Replies: 3
    Last Post: 9th May 2012, 08:25
  4. To show all items from combobox using wildcard character *
    By Sanjay123 in forum Qt Programming
    Replies: 0
    Last Post: 28th September 2011, 14:40
  5. Replies: 1
    Last Post: 10th March 2010, 10:12

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.