how to check if the a recursive scan is pointing to the folder from which it started its recursive scan

how to check if the a recursive scan is pointing to the folder from which it started its recursive scan ?

Let's say I am starting my recursive scan at folder C:\test. That is my beginfolder (beginfolder = C:\test)

Then the recursive scan scans some other folders such as C:\test\folder1.
after it is done scanning C:\test\folder1 , it continues scanning at the folder
C:\test again.( because a recursive scans scans all the files in folder alphabetically)

How do i check that the folder path that the dir is pointing to is equal to "C:\test again ?



"the current folder that dir is pointing to is not the beginfolder"

i would like to have set some condition where the current folder path that dir is pointing to is not equal to the beginfolder , so if we have a folder path that is anything different from "C:\test" for example a file path name of C:\test\folder1 would satisfy the condition.




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
                        path beginfolder = dir->path(); 

			cout << beginfolder << endl;

			cout << beginfolder << endl; 

			while (dir != end)
			{
				

				
				if (is_directory(dir->status())) //folder 
				{
					cout << "folder" << absolute(dir->path()) << endl;

					++dir;
					while (dir != end && is_regular_file(dir->status()) && "the current folder that dir is pointing to is not the beginfolder")
					{
					  cout << "file in folder" << absolute(dir->path()) << endl;
					 ++dir;
					}
				}
				else // regular file 
				{
					cout << "file" << absolute(dir->path()) << endl;
					++dir;
				}
Last edited on
What is the end result that you are trying to achieve?
To avoid infinite recursion I suggest a common approach:

Use a container (such as std::set).
Before you process a directory search it in the container. If it is not in the container add it and continue processing otherwise nothing has to be done.
starting off :

path beginfolder = dir->path();

will give me something like "C:\test\abc.cpp" , but i would like to have just the folder path which is

"C:\test" . is there any function in C++ that gives me that ?
i have defined a map which stores the extension of all the files in a folder as the key and a set of parent folder paths which contain the given extension from the key as the value.

I would like to print out this map filesto;

map<path, set<path>> filesto ;


but the problem is that my value of my map is a set.
and the "<<" operator is not defined for the set .

How can i print out the map ? I am not very sure about operator overloading.




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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

map<path, set<path>> filesto ; 

if(verbose == true)
   { 
		cout << "Detail Report" << endl;
		cout << "---------------" << endl;
	

		for (auto x : extuniq)
		{
			int counter = 0; 
			path beginfolder = dir->path(); 

			path parent = beginfolder.parent_path();

			filesto[x].insert(parent); 


			cout << parent  << endl;

			cout << parent  << endl; 

			while (dir != end)
			{
				//filepaths.insert(dir->path());

				
				if (is_directory(dir->status())) //folder 
				{
					cout << "folder" << absolute(dir->path()) << endl;

					++dir;
					while (dir != end && is_regular_file(dir->status()) )
					{
						filepaths.insert(dir->path());
						//if(filepaths.find(ext) != string::npos)
						{}

					  cout << "file in folder" << absolute(dir->path()) << endl;
					 ++dir;
					}
				}
				else // regular file 
				{
					cout << "file" << absolute(dir->path()) << endl;

					path currentfolder = absolute(dir->path()); 

					string abs = dir->path().string(); 

					string xstr = x.string(); 

					// if the current absolute path contains the extension xstr
					if (abs.find(xstr) != string::npos)
					{
						path parentfolder = currentfolder.parent_path();

						filesto[x].insert(parentfolder);

					}



					++dir;
				}

			
			}
			
		
			dir = recursive_directory_iterator(folder);
		}
	
		cout << endl;
	}



	for (auto x : filesto)
	{
		cout << x.first <<  "    " << x.second << endl; 

	}


Something like this, perhaps (the part which prints out the contents of the map is highlighted):

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
#include <iostream>
#include <experimental/filesystem>
#include <map>
#include <set>
#include <string>
namespace fs = std::experimental::filesystem ;

std::map< std::string, std::set<fs::path> >
parent_paths( fs::path dir, const std::set<std::string>& extensions )
{
    std::map< std::string, std::set<fs::path> > result ;

    for( const fs::path& p : fs::recursive_directory_iterator(dir) )
    {
        if( fs::is_regular_file(p) )
        {
            const std::string ext = p.extension().string() ;
            if( extensions.find(ext) != extensions.end() )
                result[ext].insert( p.parent_path() ) ;
        }
    }

    return result ;
}

int main()
{
    const fs::path dir = "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC" ;

    const auto ext_map = parent_paths( dir, { ".obj", ".lib", ".asm" } ) ;

    for( const auto& pair : ext_map )
    {
        std::cout << "extension: " << pair.first << '\n' ;
        for( const auto& p : pair.second ) std::cout << '\t' << p << '\n' ;
        std::cout << '\n' ;
    }
}

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