Boost::filesystem Searching...

Pages: 12
Could someone please explain how I can use the boost library to search for a directory and/or file in any directory?

How am I supposed to use an iterator? What does it return? (vector, string, etc)

Thank you for your time! (this isn't homework/scvhool work)
please??
Yeah, i know. That doesn't explain what it is. I dont half-ass stuff.

What's 'char* argv[]'? It wasnt declared, and yet it's an argument to main(). Let just jump right into it without knowing anything, huh?

Lets just start doing it without knowing why

1
2
copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type
          ostream_iterator<directory_entry>(cout, "\n"));


wont even compile, or why it doesnt copy somthing into every folder on your system, or knowing what ostream_iterator does, etc...

Please answer my previous question. Thank you.
Last edited on
wont even compile,

That's not enough information to help: give the complete source code and the compiler error. Here's that boost tutorial compiled live, online: http://liveworkspace.org/code/a342716e139beb575b0a7d725c45175a (I only hard-coded the directory it's searching since LWS doesn't allow input)
Last edited on
IWishIKnew wrote:
What's 'char* argv[]'? It wasnt declared, and yet it's an argument to main().
argv is an array of cstrings that are passes to the application at the command line. The number of arguments passed is stored in argc.
Last edited on
@naraku9333

So, why is it a char? And why do we use a pointer ('char*', instead of 'char')?
______________________________________________________________

What does directory_iterator(p) return? An array? A Vector?

What is ostream_iterator, and what does it do?

where does directory_entry come from? How was it initialized (if it was)? What is it (a string, array, vector, int, etc)?
What does directory_iterator(p) return? An array? A Vector?

It returns an iterator, to the directory, for path 'p'

What is ostream_iterator, and what does it do?

It is an iterator, that traverses an ostream object.

where does directory_iterator() come from?
Boost.Filesystem library

How was it initialized (if it was)?
In the context of the example it is the end of directory iterator.

example:
1
2
//assume myvector is a vector<int> that has 100 elements
std::for_each(myvector.begin(),myvector.end(),PrintElement());


The directory_iterator() is analogous to myvector.end()

Your real questions
where does directory_entry come from?
It is the value type of the directory iterator. It's a 'class' inside the Boost Library that "represents" an entry in a directory.

e.g. In your C:\ directory you have folders, text files, pictures, videos etc. Each one of these is a directory_entry.
Last edited on
Thank you for the help. So, why do we use 'copy()'?

Also:

so, how should i 'treat' this iterator? Is it like a vector/array, or does it have it's own properties?

when you say
an iterator, that traverses an ostream object.
, what exactly does that mean? Do i have to use an 'ostream object' every time i want to gather a list of directory entries?

Is there any possible way I could use the iterator to place the directory entries into a vector?

Thanky uo for your time! And thank you for being patient with me.
Alright, so i got Tut2 to work! Turns out, copy() was not part of the Boost Filesystem library at all... So, now I have JUST 1 MORE QUESTION:

How can I have this iterator write it's "findings" to a vector, instead of displaying them?
Here's how I've been doing it.

1
2
3
std::vector <boost::filesystem::directory_entry> ItDir;

std::copy(boost::filesystem::directory_iterator(DirectoryPath, boost::filesystem::directory_iterator(), back_inserter(ItDir));


http://www.cplusplus.com/reference/std/iterator/back_inserter/
Last edited on
So, I want to confirm somthing:

an iterator is a 'variable type' (if you will).
I have a problem: I'm creating a function to search through all directories, and it will be designed to return a vector<string> of all the matching directories it finds. It will only use a single string, such as "minecraft", or "users", and search your computer until it finds any directory with that string anywhere in its' name.

I'm having problems converting boost::filesystem::directory_entry variables into string variables for this operation. Is there any way to convert a directory entry into a string?
directory_entry has a path member which has a string member.
1
2
directory_entry d;
d.path().string();
d.path().string() translates to a string, or is it just d.string()?
It looks like either will work, I initially thought directory_entry didn't have a string method.
Edit: I'm not particularly experienced with boost so I dont know which would be recommended.
Last edited on
Alright, thank you!

I am having problems. I created a recursive algorithm to search every file/folder on the disk drive for specific keywords in the folder/file names. It works, but (it seems) every time a match is found, and recursively returned, it crashes. I dont have the debugger (i installed boost + minGW and it didnt come with a Debugger... :\ ) so I'm kind of clueless. I did insert some test code (display variables, etc...) and it all points to the recursively returned values. I don't know why It is crashing. Can someone help me out? Here is the recursive algorithm I wrote:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
vector<string> find_dir(string f_name = "", string path = "")
{
    vector<boost::filesystem::directory_entry> directories;
    vector<string> rec_matches;
    vector<string> matching_directories;
    if(keypressed() == true)
    {
        return matching_directories;
    }
    string s;  //for string operations
    boost::filesystem::directory_entry directory_entry_to_string;
    boost::filesystem::path p;
    p = path;
    if(path == "")  //default it
    {
        p = "C:\\";
    }
    copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), back_inserter(directories));
    if(directories.size() > 0)  //just a precaution
    {
        if(match(directories, f_name) == true)  //check to see if there is a match
        {
            for(unsigned int x = 0; x < directories.size(); x++)  //there us a match, add the matching dirs to a vector
            {
                directory_entry_to_string = directories[x];
                s.clear();
                s = directory_entry_to_string.path().string();
                if(s_match(s, f_name) == true)
                {
                    matching_directories.push_back(s);
                }
            }
        }
    }
    //now we go deeper into the filesystem...
    for(unsigned int x = 0; x < directories.size(); x++)
    {
        p = directories[x].path();
        if((is_matching_dir(directories[x], matching_directories) == false) && (dir(p) == true))  //making sure we dont add another of the same directory...
        {
            rec_matches.clear();
            rec_matches = find_dir(f_name, p.string());
            if(rec_matches.size() > 0)
            {
                for(unsigned int y = 0; y < rec_matches.size(); x++)
                {
                    matching_directories.push_back(rec_matches[x]);  // crashes HERE when rec_matches.size() > 1, and of course i cant figure out why...
                }
            }
        }
    }
    return matching_directories;
}


I would really appreciate any help you can give. I know it is in this function, because it always crashes in this function.
Last edited on
would boost's recursive_directory_iterator be more fitting for this task?
do I use that in the same way as the regular iterator?
Pages: 12