Solved

Search a directory for certain files

Posted on 2003-11-21
4
507 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 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