user12279137
user12279137

Reputation:

How to change files names in a folder that in a directory?

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?

Upvotes: 0

Views: 884

Answers (1)

RimeBeliskner
RimeBeliskner

Reputation: 362

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
    }
}

Upvotes: 1

Related Questions