boost::filesystem question

Quick question about the filesystem. I've made a wrapper class for the filesystem library of boost. I have made a function that scans all files and folders in a directory, this works fine; but I can not get it to also scan the folders for any more files(but it will find more folders).

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
void FileSystem::index::add(std::string directoryToAdd)
{
	if (directoryToAdd == ".")
	{
		directoryToAdd = ".\\";
	}

	boost::filesystem::path directory(directoryToAdd);

	if (boost::filesystem::exists(directory))
	{
		boost::filesystem::directory_iterator end_iterator;
		std::vector<std::string> list;
		for (boost::filesystem::directory_iterator iterator(directory); iterator != end_iterator; ++iterator)
		{
			if (boost::filesystem::is_directory(iterator->path()))
			{
				//std::cout << iterator->path().string() << "\\" << std::endl;
				add(iterator->path().string() + "\\");
			}
			list.push_back(iterator->path().string());
		}
		searchIndex.emplace(directoryToAdd, list);
	}
}


This is the relevant code. In main I put FileSystem::searchDirectory(directory) which calls this function.

Also it seems that putting . or .. at the beginning of a directory causes the program to crash. ("." - works ".\folder" - doesn't work) anyone know what this is?
To recursively scan subdirectories, use boost::filesystem::recursive_directory_iterator
http://www.boost.org/doc/libs/1_61_0/libs/filesystem/doc/reference.html#Class-recursive_directory_iterator

Use slash / as the directory separator character. Windows too recognises this generic format.
Yeah, this works perfectly, thanks!
I have one more question though! right now I am using a map of std::string and std::vector<std::string> to index the search - this would create duplicates if I searched a folder and then a folder within that

is there a better way to index searches?
The semantics of the map is not clear to me. Perhaps an example may clarify matters.
Here is an example:


Key - ./Folder/Subfolder/

Value - vector of strings:
Folder/Subfolder/file1.txt
Folder/subfulder/file2.txt


And this could cause duplicates like this:

Key - ./Folder/

Value:
Folder/Subfolder/file1.txt
Folder/subfulder/file2.txt
Folder/subfolder2/file.txt


As you can see in this example the first two entries are copied

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
#include <string>
#include <iostream>
#include <map>
#include <vector>

// #include <boost/filesystem.hpp>
// namespace fs = boost::filesystem;
#include <experimental/filesystem> 
namespace fs = std::experimental::filesystem;

using dir_map = std::map< std::string, std::vector<std::string> > ;

void add( const fs::path& path, dir_map& map, bool recursive = true )
{
    if( fs::exists(path) )
    {
        const std::string dir_str = path.string() ;
        fs::directory_iterator end ;
        for( fs::directory_iterator iter(path) ; iter != end ; ++iter )
        {
            const auto& item_path = iter->path() ;
            map[dir_str].push_back( item_path.string() ) ;
            if( recursive && fs::is_directory(item_path) ) 
            {
                map[dir_str].back() += " (directory)" ;
                add( item_path, map, true ) ;
            }
        }
    }
}

int main()
{
     dir_map map ;
     add( "C:/sandbox", map ) ;
     for( const auto& pair : map )
     {
         std::cout << pair.first << '\n' ;
         for( const auto& str : pair.second ) std::cout << "    " << str << '\n' ;
         std::cout << '\n' ;
     }
}

C:\sandbox
    C:\sandbox\Administrator (directory)
    C:\sandbox\desktop.ini
    C:\sandbox\DONT-USE.TXT

C:\sandbox\Administrator
    C:\sandbox\Administrator\DefaultBox (directory)
    C:\sandbox\Administrator\desktop.ini
    C:\sandbox\Administrator\DONT-USE.TXT

C:\sandbox\Administrator\DefaultBox
    C:\sandbox\Administrator\DefaultBox\RegHive
    C:\sandbox\Administrator\DefaultBox\RegHive.LOG1
    C:\sandbox\Administrator\DefaultBox\RegHive.LOG2
    C:\sandbox\Administrator\DefaultBox\RegHive{e5dc59b2-2041-11e6-80ef-001c42c44964}.TM.blf
    C:\sandbox\Administrator\DefaultBox\RegHive{e5dc59b2-2041-11e6-80ef-001c42c44964}.TMContainer00000000000000000001.regtrans-ms
    C:\sandbox\Administrator\DefaultBox\RegHive{e5dc59b2-2041-11e6-80ef-001c42c44964}.TMContainer00000000000000000002.regtrans-ms

http://rextester.com/GBL27978
Topic archived. No new replies allowed.