directory_iterator file_iter重命名文件夹中的文件 [英] directory_iterator file_iter to rename files in a folder

查看:76
本文介绍了directory_iterator file_iter重命名文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重命名目录中的文件.该目录中有52个文件夹.每个文件夹都有一个不同的名称,每个文件夹中都有大约40个文件.我想提取一个特定文件夹的名称,并将该名称附加到该特定文件夹中的文件名称中.当每个文件夹中只有31个或更少的文件时,它工作正常.但是,只要特定文件夹中的文件数超过31,我编写的重命名算法就会失败.我无法弄清楚为什么当有更多文件时它崩溃.如果你明白为什么...,请赐教我!我要附上代码:

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

                    }
                }
            }
        }
    }

}}

推荐答案

重命名目录列表中的文件很可能会混淆Boost的 directory_iterator 实现.

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

从文档中

警告:如果在为目录构造directory_iterator后从目录中删除文件或子目录或将其添加到目录中,则不确定是否随后进行迭代器增量导致迭代器的值是已删除或已添加的目录项.

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.

我建议分两个阶段进行尝试.在第一阶段,使用您现在拥有的代码来构建 vector< pair< string,string>.> 而不是重命名文件.然后,扫描目录后,只需遍历执行实际重命名的列表即可.

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.

这篇关于directory_iterator file_iter重命名文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆