Hi guys,

Im working on a small console app to seek out all those pesky and disk consuming .ncb files and debug folders left from VS and destroy them so the space i need to backup the files is considerably reduced.

I have got my recusrion algorithm to scan as deep as it needs to (seems to be working fine) But the issue is when i want to delete the files. I have a function that worked fine when i passed a path refrence to it, but to get recusrion i changed over to directory iterators. Using these i now get the final else case of my check function when deleting which now insists the files are neither directories or regulor files.

I cant seem to figure out after many hours toiling to find what is causing this. So if anyone could give this code a review and maybe help me out id really appreciate it. Code follows.

void scanDir(const path& dir, int numLevels)
{
	try //might get exceptions so catch them
	{

		if(!exists(dir) || !is_directory(dir)) //check path is valid and is a directory
		{
			std::cout<<"Invalid input.\n";
			exit(-1);
		}
	

		for(directory_iterator dir_iter(dir), dir_end;dir_iter != dir_end; ++dir_iter)
		{
			
			if(is_directory(dir_iter->path().leaf()))//if this is another directory dig down and look
			{
				
				cout<<padd(numLevels)<<"<DIR>"<<dir_iter->path().leaf()<<endl;
				if(checkAndRemove(dir_iter->path().leaf(),BAD_TYPES))
					continue; //if i deleted the folder i dont want to try look into it 

				scanDir(dir_iter->path().leaf(), numLevels+1);
			}
			else //this is just a file so print it
			{
				cout<<padd(numLevels)<<dir_iter->path().leaf()<<endl;
				checkAndRemove(dir_iter->path().leaf(),BAD_TYPES);

			}
		}

		std::cout<<padd(numLevels)<<"End of Folder"<<endl;

	}

	catch (const filesystem_error& ex)
	{
		cout << ex.what() << '\n';
	}

}

bool checkAndRemove(path& file, const vector<string> &unwantedTypes)
{
	vector<string>::const_iterator result;

	result = find(unwantedTypes.begin(),unwantedTypes.end(),file.extension());
	if(result!=unwantedTypes.end())
	{
		//check if its a file or directory
		if(is_directory(file))
		{
			cout<<"removing directory:"<<file.filename()<<endl;
			remove_all(file);
			folderCount++;
		}
		else if(is_regular_file(file))
		{
			cout<<"Removing file: "<<file.filename()<<endl;
			remove(file.c_str());
			fileCount++;
		}
		else
		{
			cout<<"Object is not a file or directory"<<endl;
		}
		return true;
	}
	return false;
	
}

Again many thanks in advance, if anything is unclear ill try to explian but as im new to boost im still quite shaky on my understanding of it.

Don't boost::filesystem::remove_all and boost::filesystem::remove require a full path as the argument?

AFAIK, lines 20 and 28 should have checkAndRemove( dir_iter->path(), BAD_TYPES ) instead of checkAndRemove( dir_iter->path().leaf(), BAD_TYPES )

Thanks for the answer Vijayan however.

Upon making these changes I got a compile error Saying about Unable to convert from const path, to path&.

Thankfully i realised this only required a change of my function prototype to operate on a path instead of a path& and the function now works correctly.

I appreciate your help very much :) thank you

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.