Solved

Search a directory for certain files

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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. …
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…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

738 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