0

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()));

                    }
                }
            }
        }
    }

} }

cameo
  • 77
  • 1
  • 1
  • 10
  • have you tried debugging it to see at which point and why it crashes? also, the cast `(int(*)(int))std::toupper` seems dangerous. `char`s are not `int`s. – tehlexx Jun 29 '12 at 12:56
  • Actually, its not crashing. When there are less than 31 pics. The rename works fine. Eg: If the 1st folder name is '1', then the filenames are renamed to 1_133422394789.jpg.But when the no of files are more, the re-naming becomes 1_ 1_133422394789.jpg, 1_1_1_1_12874638746.jpg etc....I think this prob has something to do with the 'end_iter' – cameo Jun 29 '12 at 13:05
  • but once there are more than 31 it crashes? have you tried with different file/folder names? maybe its not the number that's significant but the file names. Maybe you could post an example for a folder/file where it works, and one where it doesn't. – tehlexx Jun 29 '12 at 13:08
  • Yea, i tried all those possibilities. Different folders, different images/files...but beyond a certain number the algorithm fails. What i understood is that the 'end_iter' is not determining the last file. So the whole set of files in that directory is renamed again and again – cameo Jun 29 '12 at 13:11

1 Answers1

2

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.

leedm777
  • 23,444
  • 10
  • 58
  • 87
  • Yea Dave thanx... i guess the directory_iterator was getting messed up.The 'doc' explains the confusion i had while using the end_iterator...! – cameo Jul 02 '12 at 10:20