searching linux directories

Ancient Dragon 0 Tallied Votes 950 Views Share

I previously posted an MS-Windows example program about how to recursively search folders and create a list of their filename. This program is similar in concept but is for linux. It c++ and uses std::string and std::vector classes to hold the strings. When the program encounters a directory it will recursively call itself to process that directory. All the directory names are placed into a vector of vectors.

One improvement that needs to be made if using this program is to maintain the names of the directories along with the vector or filenames.

Happy programming.:)

/* This program is not copywrite and is made public domain for anyone to use
for any purpuse either private or commercial.  The only restriction is that is that the source code shall not be sold for profit but it can be freely compiled and embedded into any program in binary form.
*/

#include <iostream>
#include <vector>
#include <string>
#include <sys/types.h>
#include <dirent.h>

using namespace std;

typedef vector<string> LIST;
int seekdir(vector<LIST>& mylist, string path)
{

	DIR* dir = opendir(path.c_str());
	vector<string> lst;
	if(dir == 0)
	{
		cout << "Can not open directory '" << path << "'\n";
		return 1;
	}
//	cout << "directory '" << path << "' opened ok\n";
	struct dirent *d;
	while( (d = readdir(dir)) != 0)
	{
		if( d->d_type == DT_DIR && strcmp(d->d_name,".") != 0 &&
			strcmp(d->d_name,"..") != 0)
		{
		    string p = path + "/" + d->d_name;
		    seekdir(mylist,p);	
			
		}
		else if(d->d_type == DT_REG)
		{
			lst.push_back(d->d_name);
		}

	}
	closedir(dir);	
	mylist.push_back(lst);
	return 0;
}

int main()
{
	const char* home = getenv("HOME");
   vector<LIST> mylist;
   seekdir(mylist,home);
   int ndirs = mylist.size();
   cout << "ndirs: " << ndirs << "\n";
   int nfiles = 0;
   vector<LIST>::iterator it;
   for(it = mylist.begin(); it != mylist.end(); it++)
   {
   	   LIST& lst = *it;
   	   nfiles += lst.size();
   }
   cout << "nfiles: " << nfiles << "\n";
   return 0;
}
nanodano 0 Junior Poster in Training

I like this snippet. I always find this kind of C++ code very useful.

rajkumar veer 0 Newbie Poster

hi i m raj.
i need c source code in windows for searching directory and file in disk..i hv some problem in my source code.

thanks

venkal 0 Newbie Poster

hi i m venki

I am have two os windows xp and red hat. In my college we are working in the linux terminal in windows 2000 professional by typing telnet 192.168.2.8 in Run . I tried the same in my system but it is not working. I have to switch form XP to linux ever time. Kindly how to over come the problem

Thanks

Osama Mehtab -1 Newbie Poster

thanks

i will try understanding it
and'
will
ask u again
if got any query inthis program

:)

flavour_of_bru 0 Newbie Poster

Hi,

I am working on the same part of the logic and got stuck here. The above code will work for linux, but I am working on windows platform.

I have seen in the post that you already gave such an example in the previous post, I searched for it but could not find it.

Can you please send me the link to that post or else if you can help me with the same in windows, that would be really great.

Thanks!!

majestic0110 187 Nearly a Posting Virtuoso

very nice work!

Ashishinani1756 -7 Newbie Poster

thnks

sonsofliberty84 0 Newbie Poster

I got this code to work how I wanted to, and I was wondering if there was a way to get rid of the parent directory displays '.' and '..'.

int seekdir(vector<LIST>& mylist,vector<string> &file, string path)
{

    DIR* dir = opendir(path.c_str());
    vector<string> lst;
    struct dirent *d;
    
    if(dir == 0)
    {
        cout << "Can not open directory '" << path << "'\n";
        return 1;
    }
//    cout << "directory '" << path << "' opened ok\n";
    
    while( (d = readdir(dir)) != NULL)
    {
        if( d->d_type == DT_DIR && strcmp(d->d_name,".") != 0 &&
            strcmp(d->d_name,"..") != 0)
        {
            string p = path + "/" + d->d_name;
            seekdir(mylist,file,p);    

            lst.push_back(string(d->d_name));
        }
        else if(d->d_type == DT_REG)
        {
            lst.push_back(string(d->d_name));
        }
        file.push_back(string(d->d_name));
    }
    closedir(dir);    
    mylist.push_back(lst);
    return 0;
}

I'm pretty sure this is where I would do it or it's here

for (unsigned int i = 0; i < files.size();i++)
    {
	cout << files[i] << endl;
    }

I basically want to display the files for the folder that is passed into the program, but I don't want the parent directories '.' and '..' because they are irrelevant to my program. This is the link to the forum page I got this from. The bottom example is mixed with the code snippet provided by Ancient Dragon.
http://www.daniweb.com/forums/thread87733.html

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I'm not sure what you mean by that -- "." and ".." should not be processed according to the if statement on line 17 that you posted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.