Thread: Filter directory search results.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    Filter directory search results.

    Hi all, I have a function that returns the contents of a directory in a string vector. It works perfectly but what I want to do is filter out the results for a certain file type. I just wanted to know if there was anything already available to do this? Or would I just have to take the last 4 characters of the string and compare it? Below is the function I am using to get the contents of a directory.

    Code:
    int getdir (std::string dir, std::vector<std::string> &files)
    {
        DIR *dp;
        struct dirent *dirp;
        if((dp  = opendir(dir.c_str())) == NULL) {
            std::cout << "Error(" << errno << ") opening " << dir << std::endl;
            return errno;
        }
    
        while ((dirp = readdir(dp)) != NULL) {
            files.push_back(std::string(dirp->d_name));
        }
        closedir(dp);
        return 0;
    }

    EDIT:
    I have come up with this... but its not perfect… but it works…
    so again, is there a better way to do this?


    Code:
    int getdir (std::string dir, std::vector<std::string> &files, std::string filetype)
    {
        DIR *dp;
        struct dirent *dirp;
        if((dp  = opendir(dir.c_str())) == NULL) {
            std::cout << "Error(" << errno << ") opening " << dir << std::endl;
            return errno;
        }
    
        while ((dirp = readdir(dp)) != NULL) {
            if (std::string(dirp->d_name).find(filetype)!=std::string::npos)
               files.push_back(std::string(dirp->d_name));
        }
        closedir(dp);
        return 0;
    }
    Last edited by Loic; 08-17-2008 at 08:51 PM.

  2. #2
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    I would suggest substr so that you're comparing to the end instead of searching the whole string. If you''re looking for .txt files, foo.txt.bar would match with the current function

  3. #3
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    ok so i have come up with this...
    Code:
    int getdir (std::string dir, std::vector<std::string> &files, std::string filetype)
    {
        DIR *dp;
        struct dirent *dirp;
        if((dp  = opendir(dir.c_str())) == NULL) {
            std::cout << "Error(" << errno << ") opening " << dir << std::endl;
            return errno;
        }
    
        while ((dirp = readdir(dp)) != NULL) {
            if (std::string(dirp->d_name).substr((std::string(dirp->d_name).length()-filetype.length()),filetype.length()).find(filetype)!= std::string::npos )
               files.push_back(std::string(dirp->d_name));
        }
        closedir(dp);
        return 0;
    }

    it compiles fine, but when i try to run it i get this error...
    Code:
    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Wearing the hat of guessing, I would suspect that
    Code:
    std::string(dirp->d_name).length()-filetype.length())
    becomes negative - for example, the filename "." and ".." to be shorter than some filetypes.

    As a side note:
    std::string(dirp->d_name) is repeated three times in this line:
    Code:
    if (std::string(dirp->d_name).substr((std::string(dirp->d_name).length()-filetype.length()),filetype.length()).find(filetype)!= std::string::npos )
               files.push_back(std::string(dirp->d_name));
    You should probably create a temporary variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by matsp View Post
    Wearing the hat of guessing, I would suspect that
    Code:
    std::string(dirp->d_name).length()-filetype.length())
    becomes negative - for example, the filename "." and ".." to be shorter than some filetypes.

    As a side note:
    std::string(dirp->d_name) is repeated three times in this line:
    Code:
    if (std::string(dirp->d_name).substr((std::string(dirp->d_name).length()-filetype.length()),filetype.length()).find(filetype)!= std::string::npos )
               files.push_back(std::string(dirp->d_name));
    You should probably create a temporary variable.

    --
    Mats
    Ah cheers for that. if I use that function with out the file type filtering the first two results are always “.” and “..”

    How could I get past this problem?

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by Loic View Post
    Ah cheers for that. if I use that function with out the file type filtering the first two results are always “.” and “..”

    How could I get past this problem?
    Use rfind?

  7. #7
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by Loic View Post
    Ah cheers for that. if I use that function with out the file type filtering the first two results are always “.” and “..”

    How could I get past this problem?
    Just put up a check comparing the string to . and .. before doing anything else

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Loic View Post
    Ah cheers for that. if I use that function with out the file type filtering the first two results are always “.” and “..”

    How could I get past this problem?
    Perhaps check if the length of the string is shorter than the length of the extension you are looking for? Then it can not possibly be a match, so no point in trying.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Just a recommendation for the boost::filesystem library if you're going to do a lot of this kind of work.

  10. #10
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by rags_to_riches View Post
    Just a recommendation for the boost::filesystem library if you're going to do a lot of this kind of work.
    Cheers that looks good… ill have a read of that, but I still wouldn’t mind trying to get this other function I have to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Big help in Astar search code...
    By alvifarooq in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:38 AM
  4. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM