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