Solved

Search a directory for certain files

Posted on 2003-11-21
4
487 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
  • 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 125 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now