0

I am writing a program using C++ and Boost::filesystem. The program is supposed to take pictures in a given directory and move them to folders. Each folder is supposed to only hold a given number of pictures.

#include<string>
#include<boost/filesystem.hpp>

using namespace std;
using namespace boost::filesystem;

vector<path> new_folders; //vector of paths that will be used to copy things
//I know a global variable is a bad idea, but this is just a simplified example of my program    

void someFunction(path somePath)
{
  directory_iterator iter(somePath);
  directory_iterator end_iter;

  int count = 0;//used in the naming of folders
  while(iter != end_iter)
  {
    string parentDirectory = iter->path().string(); 
    string newFolder = "\\Folder " + to_string(count+1);

    parentDirectory.append(newFolder);
    path newDir = parentDirectory;

    create_directory(newDir);//create new folder in parent folder
    new_folders.push_back(newDir); //add path to vector

    count++;
    iter++;
  }
}



void fill_folders(path pic_move_from, const int MAXIMUM)
{
  //this iterator does not account for the new folders that were made      
  //-------------------- HERE IS WHERE the problem is located
  directory_iterator iterate(pic_move_from);
  directory_iterator end_iter;

  //fill the new folders with pictures
  for (int folderNum = 0; folderNum < new_folders.size(); folderNum++)
  {
    path newFolder = new_folders.at(folderNum);
    int loopCount = 0; //for the following while loop

    while (loopCount != MAXIMUM && iterate != end_iter)
    {
      if(is_regular_file(*iterate) && img_check(iterate))//item must be a picture to be copied
      { 
        create_copy_multifolder(iterate, newFolder);
      }//end if

      iterate++;
      //the loopCount in the while loop condition should be the max number of folders

      loopCount++;
    }//end while loop
  }//end for loop
}//end fill_folders function


int main()
{
  path myPath = "C:\\Users\\foo";
  const int MAX = 2; //maximum number of pictures per folder

  someFunction(myPath);
  fill_folders(myPath, MAX); 

  return 0;
}

The path pic_move_from was used in another function. This other function used a directory iterator for this path, and in that same function directories were added to the directory referenced by path pic_move_from. I tried to make a new iterator for this directory so that I could move any pictures in the directory to the newly added subdirectories. However, the new directory_iterator is not "updated" to work with the new entries in the directory. So, how do you "update" the directory_iterator?

UPDATE: I tried to simplify this code as much as possible so I came up with the test/example below. And this example works just fine and prints out the new folder during the second iteration, so I'll have to double check everything in the original code.

string pathToFile = "C:\\foo";
path myPath();

directory_iterator iter(pathToFile);
directory_iterator end_iter;

while (iter != end_iter)
{
    cout << endl << iter->path().filename().string() << endl;
    iter++;
}

string pathToNew = pathToFile;
pathToNew.append("\\Newfolderrrrr");
create_directory(pathToNew);

directory_iterator iterate(pathToFile);
directory_iterator end_iterate;

while (iterate != end_iterate)
{
    cout << endl << iterate->path().filename().string() << endl;
    iterate++;
}
Wheathin
  • 98
  • 8
  • It is unclear what you are asking. Provide a [MCVE]. – Yakk - Adam Nevraumont Jul 21 '17 at 18:48
  • @Yakk I tried to clean it up some. – Wheathin Jul 21 '17 at 19:28
  • 1
    Now be less vague. "in another function" -- if you mean `someFunction` say its name. Next, *simplify*. Your code does a lot of stuff; what can you strip out *and still get the same symptoms*. Ie, skip iterating in `someFunction`, just create *one* new directory. Problem still occur? Awesome, case is simpler. Repeat until you have a really simple case. – Yakk - Adam Nevraumont Jul 21 '17 at 19:44
  • Can you separate the discovery and manipulation? Can you prepare actions before committing? – sehe Jul 21 '17 at 20:33

1 Answers1

0

It turns out that there was no issue with the directory iterators or how I was using them.

I had another flaw in my code which was strictly a logical error.

The solution was a simple rearrangement of a few blocks of code.

Sorry for all the confusion.

Daniel Trugman
  • 8,186
  • 20
  • 41
Wheathin
  • 98
  • 8