Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Search a directory for certain files

Posted on 2003-11-21
4
Medium Priority
?
539 Views
Last Modified: 2010-05-18
Hello,

I need to recursively search a local directory for all files ending with ".tgz"   Can anybody tell me how to do this?

Any ideas are highly appreciated.

Thanks,

David
0
Comment
Question by:Davidwy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9799524
Use

#include <sys/types.h>
#include <dirent.h>
#include <string>
#include <iostream>
using namespace std;

bool search_for_file ( const string& sStartDir, const string& sFile, string& sFound) {

    cout << "checking " << sStartDir.c_str () << endl;

    DIR* pDir = opendir ( sStartDir.c_str ());

    if ( !pDir) return false;

    dirent* pEntry;

    while ( pEntry = readdir ( pDir)) {

        cout << "found " << pEntry->d_name << endl;

        if ( DT_DIR & pEntry->d_type && strcmp ( pEntry->d_name, ".") && strcmp ( pEntry->d_name, "..")) {

            string sSubDir = sStartDir + string ( "/") + string ( pEntry->d_name);

            if ( search_for_file ( sSubDir, sFile, sFound)) {

                return true;
            }
        }

        // does the file match?
        if ( !strcmp ( pEntry->d_name, sFile.c_str ())) {

            sFound = sStartDir + string ( "/") + string ( pEntry->d_name);

            return true;
        }
    }

    return false;
}

int main () {

    string sFound = "notfound";

    search_for_file ( ".", "thefile", sFound);

    cout << sFound.c_str ();
}
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9799537
Ooops, the abouve searches for a single file - if you want to find multiple files wit a substring search, use

#include <sys/types.h>
#include <dirent.h>
#include <string>
#include <iostream>
using namespace std;

void search_for_file ( const string& sStartDir, const string& sFile, list<string>& lstFound) {

    cout << "checking " << sStartDir.c_str () << endl;

    DIR* pDir = opendir ( sStartDir.c_str ());

    if ( !pDir) return false;

    dirent* pEntry;

    while ( pEntry = readdir ( pDir)) {

        cout << "found " << pEntry->d_name << endl;

        if ( DT_DIR & pEntry->d_type && strcmp ( pEntry->d_name, ".") && strcmp ( pEntry->d_name, "..")) {

            string sSubDir = sStartDir + string ( "/") + string ( pEntry->d_name);

            search_for_file ( sSubDir, sFile, sFound)) {

        }

        // does the file match?
        if ( !strstr ( pEntry->d_name, sFile.c_str ())) {

            string sFound = sStartDir + string ( "/") + string ( pEntry->d_name);

            lstFound.push_back ( sFound);
        }
    }
}
0
 
LVL 4

Expert Comment

by:YuriPutivsky
ID: 9800673
// set the directory where you are looking for files as the current one
if (!_chdir("c:\\search"))
   // something wrong

struct _finddata_t c_file;
long hFile;

if ((hFile = _findfirst( "*.tgz", &c_file )) == -1)
       // no files were found
else
{
     // find the first file
     // c_file.name contains the file name
     // looking for others
     while(!_findnext(hFile, &c_file))
     {
         // find the next file
         // c_file.name contains the file name
     }

     // don't fordet to release a handle
     _findclose(hFile);
}
0
 

Author Comment

by:Davidwy
ID: 9810487
HelloJkr and YurPutivsky,

As I work on Linux, I accept Jkr's comment as my answer.  Thanks to you all for your comments!

Regards,

David
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question