Question

I wanted to rename the files in a directory.There are 52 folders in the directory. Each folder has a different name and has around 40 files in each of them.I wanted to extract the name of a particular folder and attach that name to the name of the files in that particular folder. It worked fine, when there was only 31 or less files in each folder. But whenever the number of files in a particular folder was above 31 the rename algorithm i wrote failed. I am not able to figure out why it crashes when there are more files. Do enlighten me if u understand why...! I'm attaching the code:

int main( int argc, char** argv ){
directory_iterator end_iter;
directory_iterator file_itr;

string inputName;
string checkName;
inputName.assign(argv[1]);


if (is_directory(inputName))
{

    for (directory_iterator dir_itr(inputName); dir_itr != end_iter; ++dir_itr)
    {
        if (is_directory(*dir_itr))
        {
            for (directory_iterator file_itr(*dir_itr); file_itr != end_iter; ++file_itr)
            {
                string folderName(dir_itr->path().filename().string());
                if (is_regular_file(*file_itr)) 
                {
                    std::string fileType = file_itr->path().extension().string();
                    std::transform(fileType.begin(), fileType.end(), fileType.begin(), (int(*)(int))std::toupper);
                    if (fileType == ".JPG" || fileType == ".JPEG" || fileType == ".JPG" || fileType == ".PGM") 
                    {
                        string filename(file_itr->path().string());
                        string pathName(file_itr->path().parent_path().string());
                        string oldName(file_itr->path().filename().string());

                        cout << folderName << endl;
                        folderName += "_";
                        folderName += oldName;

                        string newPathName = pathName + "\\" + folderName;
                        cout << pathName <<"\\"<< folderName << endl;

                        //RENAMING function
                        rename(file_itr->path(), path(newPathName.c_str()));

                    }
                }
            }
        }
    }

} }

Was it helpful?

Solution

It's likely that Boost's directory_iterator implementation is getting confused by you renaming files that are in the directory listing.

From the docs:

Warning: If a file or sub-directory is removed from or added to a directory after the construction of a directory_iterator for the directory, it is unspecified whether or not subsequent incrementing of the iterator will ever result in an iterator whose value is the removed or added directory entry.

I recommend trying it in two phases. In the first phase, use the code you have now to build a vector<pair<string, string> > instead of renaming the file. Then, once you've scanned the directory, it should just be a matter of iterating through the list performing the actual renames.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top