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++;
}