LobsterGhost
LobsterGhost

Reputation: 1

C++ Program for Renaming Multiple Files at Once

I have a directory full of JPG files named Slide1, Slide2, Slide3, etc and I want to rename them to a list of names I wrote in a txt file titled "names.txt". Each new name is on one line, so basically I need to read this txt file line-by-line and make each line the new name of a JPG file. I will need to do this for multiple directories, but I expect each one to only contain a max of 60 JPGs (I will use the same names.txt file to rename the JPGs in each directory because it does not matter if there are duplicate JPG names between the directories, it only matters that the no two JPGs in the same directory have the same name).

This is the code I have so far. I know one problem is that char oldname[] and char newname[] can't use the + operator. So how do I get char oldname[] for each JPG to be Slide1, Slide2, etc? As for char newname[], I thought maybe I need to read each line of names.txt as a string and then convert that string to char and set it as char newname[] but I don't know how to do that.

#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int myNum;
    cout << "How many slides are there? Max 60." << endl;   
    cin >> myNum;

    if (myNum > 60)
           cout << "Add more names to names.txt in lyrics folder, then rerun program." << endl; 

    if (myNum <= 60)
    {       
        int nextNum = 1;
        fstream readname;
        readname.open("names.txt",ios::in);
        if (readname.is_open())
        {
            string line;
            while (getline(readname,line))
            {
                char oldname[] = "Slide" + nextNum + ".JPG";
                char newname[] = line + ".JPG"; 
                nextNum = nextNum++;
                if (nextNum == myNum+1)
                    break;
            }
            readname.close();
        }
    }
    return 0;
}

If anyone can explain how to do this using string instead of char, that would be fine too. This isn't an assignment, it's just a personal project, so there are no limits to what methods I can use.

Upvotes: 0

Views: 1579

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 598029

You should not be using char[] arrays at all. Just use std::string instead:

std::string oldname = "Slide" + std::to_string(nextNum) + ".JPG";
// Or, if you are not using C++11 or later:
/*
std::ostringstream oss;
oss << "Slide" << nextNum << ".JPG";
std::string oldname = oss.str();
*/

string newname = line + ".JPG";

Upvotes: 0

Rajkumar Ananthu
Rajkumar Ananthu

Reputation: 404

From C++11 we have a function std::to_string this takes a numeric value and returns a std::string object. More info at https://en.cppreference.com/w/cpp/string/basic_string/to_string

Using that you can convert your code(in while loop) as follows:

std::string oldName = std::string("Slide") + std::to_string(nextNum) + ".JPG";
std::string newName = line + ".JPG";

Hope this helps,

Thanks

Upvotes: 3

Related Questions