Jump to content
Larry Ullman's Book Forums

Recursive Directory Iterator Example

Recommended Posts

Hi, Larry OK'd the posting of this code example which I eventually got to work.  The environment was Windows 10, Visual Studio 2017 with the C++17 language in a Windows Form. I wanted to select a directory that had sub-directories and process jpg files in either the top-level directory or and of its sub-directories.  I used recursive_directory_iterator to get access to the directories.  Here's the code just FYI in case anyone else is crazy enough like me to want to do this.

private: System::Void btnSelectDir_Click(System::Object^  sender, System::EventArgs^  e)
	{	// the 'select the root directory' button was clicked
		FolderBrowserDialog^ folderBrowserDialog1 = gcnew FolderBrowserDialog;
		directory_list = gcnew List<String^>(20);	// initially allow for 20 entries but the capacity will expand automatically if needed
		directory_count = 0;
		total_image_files_count = 0;
		folderBrowserDialog1->SelectedPath = "E:\\Documents\\";	// adjust as necessary
		// the following control allows the selection of the input root directory
		System::Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog();
		if (result == System::Windows::Forms::DialogResult::OK)
				this->txtDirName->Text = folderBrowserDialog1->SelectedPath;
				selected_root_directory = folderBrowserDialog1->SelectedPath;	// selected_root_directory is a managed string
				// we have selected a root directory OK
				// convert it to the format required by recursive_directory_iterator
				marshal::marshal_context context;	// sets the context - needed - but don't know why!
				std::string unmanaged_selected_path = context.marshal_as<std::string>(selected_root_directory); // Managed string to standard string
				fs::path selected_path = unmanaged_selected_path;												// standard string to path
				// first see if the selected root directory contains any jpg files - if so add it to the list
				if (the_directory_contains_at_least_one_jpg_file(selected_path))
					{	// add the directory to the list
						directory_list->Add(selected_root_directory);	// directory_list has (and needs) global scope
					}	// end the selected root directory contained at least one jpg type file
				// now see if the selected root directory contains any sub-directories
				for (auto& p : fs::recursive_directory_iterator(selected_path))
					{	// 'p' is the path data returned from each iteration - it may reference a sub-directory or a file
						if (fs::is_directory(p.path()))
							{	// we have a directory
								std::string path_details = p.path().string();						// convert the path details to a standard string
								String^ managed_path_details = gcnew String(path_details.c_str());	// convert the standard string to a managed string

fs was the namespace I set up for the file system.

Cheers from Oz.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...