0

I'm setting up my C++ project, the project is about rename files in any location.

I'm using filesystem library.

The project run successfully, and I didn't get any errors, but when I put a full path ( example, Documents ), it's not changing the files that in a folder. For example, I got a folder inside my downloads directory, I have a folder that is called "myfolder". Inside this folder I have 2 txt files, my program changes the name of the all files that in the downloads but not inside the "myfolder" folder.

string dirPath = "C:\\Users\\" + pcuser + "\\Downloads";
auto path = fs::path(dirPath);
auto dir = fs::directory_iterator(path);

for (auto& file : dir)
{
        int Filename = rand() % 2342;
        rename(file.path(), fs::path(dirPath + "\\" + to_string(Filename)).c_str());
        Filename++;
}

I want to change the files that are in a folder too. How can I do this?

1 Answer 1

1

There is a std::filesystem::recursive_directory_iterator so you can just use it and renamed entity when it's a file

for(auto& p: fs::recursive_directory_iterator(dirname))
{
    if (fs::is_regular_file(p))
    {
        //do rename
    }
}
2
  • Could you please connect it with my code? I didn't really understand where to put and how .. I recently started with this library
    – user12279137
    Commented Oct 27, 2019 at 13:12
  • @MasterBootRecord, I'm not quite sure that it's right way to do, see this question - directory_iterator file_iter to rename files in a folder. So I recommend also implement two-phase algorithm. Commented Oct 27, 2019 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.