Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <cwchar>
  3. #include <string>
  4. #include <map>
  5. #include <vector>
  6. #include <fileref.h>
  7. #include <experimental\filesystem>
  8.  
  9. namespace fs = std::experimental::filesystem;
  10.  
  11. std::vector<fs::path> getAllFileDirectory(fs::path path) {
  12.     std::vector<fs::path> listPath;
  13.     for (auto& p : fs::recursive_directory_iterator(path))
  14.         listPath.push_back(p.path());
  15.     return listPath;
  16. }
  17.  
  18. std::pair<std::wstring::iterator, std::wstring::iterator> getInThe(wchar_t caractere, std::wstring::iterator begin, std::wstring::iterator end) {
  19.     std::wstring::iterator origin = begin;
  20.     if (*begin != caractere)
  21.         return std::make_pair(origin, origin);
  22.     begin++;
  23.     for (; begin <= end; begin++) {
  24.         if (*begin == caractere) {
  25.             return std::make_pair(++origin, begin);
  26.         }
  27.     }
  28.     return std::make_pair(origin, origin);
  29. }
  30.  
  31. std::wstring verifyTagForPath(std::wstring path) {
  32.     for (size_t i = 0; i < path.size(); i++) {
  33.         if (path[i] == L'\\') {
  34.             path[i] = L'&';
  35.         }
  36.         else if (path[i] == L'/') {
  37.             path[i] = L'&';
  38.         }
  39.         else if (path[i] == L'?') {
  40.             path[i] = L' ';
  41.         }
  42.         else if (path[i] == L'*') {
  43.             path[i] = L' ';
  44.         }
  45.         else if (path[i] == L':') {
  46.             path[i] = L' ';
  47.         }
  48.         else if (path[i] == L'<') {
  49.             path[i] = L' ';
  50.         }
  51.         else if (path[i] == L'>') {
  52.             path[i] = L' ';
  53.         }
  54.         else if (path[i] == L'|') {
  55.             path[i] = L' ';
  56.         }
  57.         else if (path[i] == L'\"') {
  58.             path[i] = L' ';
  59.         }
  60.     }
  61.     return path;
  62. }
  63.  
  64. std::wstring nameByTag(std::wstring formating, TagLib::Tag* tag) {
  65.  
  66.     bool oneChange = false;
  67.  
  68.     for (uint64_t i = 0; i < formating.size(); i++) {
  69.        
  70.         std::pair<std::wstring::iterator, std::wstring::iterator> iterators = getInThe(L'%', formating.begin() + i, formating.end());
  71.  
  72.         if (iterators.first != iterators.second) {
  73.  
  74.             std::wstring command(iterators.first, iterators.second);
  75.             formating.erase(iterators.first - 1, iterators.second + 1);
  76.  
  77.             if (command == L"album" && tag->album() != TagLib::String::null) {
  78.                 oneChange = true;
  79.                 std::wstring album = verifyTagForPath(tag->album().toWString());
  80.                 formating.insert(iterators.first - 1, album.begin(), album.end());
  81.             }
  82.             else if (command == L"artist" && tag->artist() != TagLib::String::null) {
  83.                 oneChange = true;
  84.                 std::wstring artist = verifyTagForPath(tag->artist().toWString());
  85.                 formating.insert(iterators.first - 1, artist.begin(), artist.end());
  86.             }
  87.             else if (command == L"title" && tag->title() != TagLib::String::null) {
  88.                 oneChange = true;
  89.                 std::wstring title = verifyTagForPath(tag->title().toWString());
  90.                 formating.insert(iterators.first - 1, title.begin(), title.end());
  91.             }
  92.             else if (command == L"year" && tag->year() != 0) {
  93.                 oneChange = true;
  94.                 std::wstring year = std::to_wstring(tag->year());
  95.                 formating.insert(iterators.first - 1, year.begin(), year.end());
  96.             }
  97.             else if (command == L"track" && tag->track() != 0) {
  98.                 oneChange = true;
  99.                 std::wstring track = std::to_wstring(tag->track());
  100.                 formating.insert(iterators.first - 1, track.begin(), track.end());
  101.             }
  102.  
  103.         }
  104.  
  105.     }
  106.  
  107.     if(oneChange)
  108.         return formating;
  109.     return std::wstring(L"UNKNOW");
  110. }
  111.  
  112.  
  113. std::map<fs::path, fs::path> formateAllFileByTag(std::vector<fs::path> path, std::wstring formating) {
  114.     std::map<fs::path, fs::path> mapByChange;
  115.     for (auto& i : path) {
  116.         fs::path originPath = i;
  117.         TagLib::FileRef test(i.generic_wstring().c_str());
  118.         if (!test.isNull()) {
  119.             std::wstring extension = i.extension();
  120.             i.remove_filename();
  121.             i /= nameByTag(formating, test.tag());
  122.             i += extension;
  123.         }
  124.         mapByChange.emplace(std::make_pair(originPath, i));
  125.     }
  126.     return mapByChange;
  127. }
  128.  
  129.  
  130. void renameAllFile(std::map<fs::path, fs::path> list) {
  131.     for (auto& i : list) {
  132.         if (i.first != i.second) {
  133.             std::wcout << i.first << "  TO :    " << i.second << std::endl;
  134.             if(!fs::exists(i.second))
  135.                 fs::rename(i.first, i.second);
  136.             else {
  137.                 uint64_t number = 0;
  138.                 std::wstring extension = i.second.extension().wstring();
  139.                 fs::path bufI = i.second;
  140.                 while (fs::exists(bufI)) {
  141.                     bufI = i.second;
  142.                     bufI.replace_extension("");
  143.                     bufI += std::to_wstring(number);
  144.                     bufI += extension;
  145.  
  146.                     number++;
  147.                 }
  148.                 std::wcout << i.first << "  FINALY TO : " << bufI << std::endl;
  149.                 fs::rename(i.first, bufI);
  150.             }
  151.         }
  152.     }
  153. }
  154.  
  155. wchar_t* toWchar(char* string) {
  156.     size_t size = strlen(string)+1;
  157.     wchar_t* buf = new wchar_t[size];
  158.     size_t tmp = 0;
  159.     mbstowcs_s(&tmp, buf, size, string, size);
  160.     return buf;
  161. }
  162.  
  163.  
  164. int main(int argc, char* argv[]) {
  165.  
  166.     if (argc == 2) {
  167.  
  168.  
  169.         renameAllFile(formateAllFileByTag(getAllFileDirectory(".\\"), toWchar(argv[1])));
  170.  
  171.     }
  172.     else {
  173.         std::wcout << L"Please type : renamer.exe [your config here]\n%album%\n%title%\n%artist%\n%year%\n%track%" << std::endl;
  174.     }
  175.  
  176.    
  177.  
  178.  
  179.     return 0;
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement