Advertisement
Scarlet

Lazy Rename

Sep 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <filesystem>
  6. namespace fs = std::experimental::filesystem;
  7.  
  8. using namespace std;                                // Typing std:: before everything is messy and unnecessary for this kinda thing
  9.  
  10. int main()
  11. {
  12.     string path;                    // Path to series directory
  13.     int startNumber;                // The index of the first episode of a folder (eg, Attack on Titan S2E1 is actually episode 26)
  14.     string epNumberTxt;             // The current episode, including zero padding
  15.     string seriesName;              // Name of the series
  16.     string newFileName;             // The final assembled file name
  17.     string fileExtension;           // Extension of the file (can probably be grabbed automatically, might do that at some point)
  18.     int totalEpisodes;              // Used to find how many zeros to pad with
  19.  
  20.     cout << "Enter the path of your series: ";
  21.     getline(cin, path);
  22.     if (path[path.length() - 1] != '\\') path = path + "\\";
  23.  
  24.     cout << "Enter the series name: ";
  25.     getline(cin, seriesName);
  26.  
  27.     cout << "Enter the video format (eg, .mkv): ";
  28.     getline(cin, fileExtension);
  29.     if (fileExtension[0] != '.') fileExtension = "." + fileExtension;
  30.  
  31.     cout << "Enter the number of the first episode present: ";
  32.     cin >> startNumber;
  33.  
  34.     cout << "Enter the total number of episodes for the series: ";
  35.     cin >> totalEpisodes;
  36.  
  37.     fstream fileNames;
  38.     fileNames.open("Names.txt");
  39.     vector<string> nameList;
  40.     string line;
  41.     while (getline(fileNames, line))
  42.     {
  43.         for (int i = 0; i < line.length(); i++)
  44.         {
  45.             if (line[i] == '\\' ||                  // Check for prohibited characters in the new file name before applying it
  46.                 line[i] == '/'  ||
  47.                 line[i] == ':'  ||
  48.                 line[i] == '*'  ||
  49.                 line[i] == '?'  ||
  50.                 line[i] == '"'  ||
  51.                 line[i] == '<'  ||
  52.                 line[i] == '>'  ||
  53.                 line[i] == '|')
  54.  
  55.                 line[i] = '_';                      // Replace with underscore
  56.         }
  57.  
  58.         nameList.push_back(line);
  59.     }
  60.  
  61.     int iteration = 0;
  62.     for (auto & p : fs::directory_iterator(path))   // For each file in the directory
  63.     {
  64.         if (nameList.size() > iteration)
  65.         {
  66.             if (totalEpisodes >= 100)           // If the series is 100+ episodes, 0-9 need "00", and 10-99 need "0"
  67.             {
  68.                 if      (startNumber + iteration < 10) epNumberTxt = "00" + to_string(startNumber + iteration);
  69.                 else if (startNumber + iteration < 100) epNumberTxt = "0" + to_string(startNumber + iteration);
  70.                 else     epNumberTxt = to_string(startNumber + iteration);
  71.             }
  72.  
  73.             else if (totalEpisodes >= 10)       // If the series is 10+ episodes, 1-9 need "0"
  74.             {
  75.                 if   (startNumber + iteration < 10) epNumberTxt = "0" + to_string(startNumber + iteration);
  76.                 else  epNumberTxt = to_string(startNumber + iteration);
  77.             }
  78.  
  79.             newFileName = path + seriesName + " " + epNumberTxt + " - " + nameList[iteration] + fileExtension;      // Assembles final file name, path is required else it'll be placed
  80.             rename(p, newFileName.c_str());                                                                         // in the same folder as the program
  81.  
  82.             iteration++;
  83.         }  
  84.     }
  85.        
  86.     cout << endl;
  87.     system("Pause");
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement