?
Solved

Search a directory for certain files

Posted on 2003-11-21
4
Medium Priority
?
534 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

765 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