Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cwchar>
- #include <string>
- #include <map>
- #include <vector>
- #include <fileref.h>
- #include <experimental\filesystem>
- namespace fs = std::experimental::filesystem;
- std::vector<fs::path> getAllFileDirectory(fs::path path) {
- std::vector<fs::path> listPath;
- for (auto& p : fs::recursive_directory_iterator(path))
- listPath.push_back(p.path());
- return listPath;
- }
- std::pair<std::wstring::iterator, std::wstring::iterator> getInThe(wchar_t caractere, std::wstring::iterator begin, std::wstring::iterator end) {
- std::wstring::iterator origin = begin;
- if (*begin != caractere)
- return std::make_pair(origin, origin);
- begin++;
- for (; begin <= end; begin++) {
- if (*begin == caractere) {
- return std::make_pair(++origin, begin);
- }
- }
- return std::make_pair(origin, origin);
- }
- std::wstring verifyTagForPath(std::wstring path) {
- for (size_t i = 0; i < path.size(); i++) {
- if (path[i] == L'\\') {
- path[i] = L'&';
- }
- else if (path[i] == L'/') {
- path[i] = L'&';
- }
- else if (path[i] == L'?') {
- path[i] = L' ';
- }
- else if (path[i] == L'*') {
- path[i] = L' ';
- }
- else if (path[i] == L':') {
- path[i] = L' ';
- }
- else if (path[i] == L'<') {
- path[i] = L' ';
- }
- else if (path[i] == L'>') {
- path[i] = L' ';
- }
- else if (path[i] == L'|') {
- path[i] = L' ';
- }
- else if (path[i] == L'\"') {
- path[i] = L' ';
- }
- }
- return path;
- }
- std::wstring nameByTag(std::wstring formating, TagLib::Tag* tag) {
- bool oneChange = false;
- for (uint64_t i = 0; i < formating.size(); i++) {
- std::pair<std::wstring::iterator, std::wstring::iterator> iterators = getInThe(L'%', formating.begin() + i, formating.end());
- if (iterators.first != iterators.second) {
- std::wstring command(iterators.first, iterators.second);
- formating.erase(iterators.first - 1, iterators.second + 1);
- if (command == L"album" && tag->album() != TagLib::String::null) {
- oneChange = true;
- std::wstring album = verifyTagForPath(tag->album().toWString());
- formating.insert(iterators.first - 1, album.begin(), album.end());
- }
- else if (command == L"artist" && tag->artist() != TagLib::String::null) {
- oneChange = true;
- std::wstring artist = verifyTagForPath(tag->artist().toWString());
- formating.insert(iterators.first - 1, artist.begin(), artist.end());
- }
- else if (command == L"title" && tag->title() != TagLib::String::null) {
- oneChange = true;
- std::wstring title = verifyTagForPath(tag->title().toWString());
- formating.insert(iterators.first - 1, title.begin(), title.end());
- }
- else if (command == L"year" && tag->year() != 0) {
- oneChange = true;
- std::wstring year = std::to_wstring(tag->year());
- formating.insert(iterators.first - 1, year.begin(), year.end());
- }
- else if (command == L"track" && tag->track() != 0) {
- oneChange = true;
- std::wstring track = std::to_wstring(tag->track());
- formating.insert(iterators.first - 1, track.begin(), track.end());
- }
- }
- }
- if(oneChange)
- return formating;
- return std::wstring(L"UNKNOW");
- }
- std::map<fs::path, fs::path> formateAllFileByTag(std::vector<fs::path> path, std::wstring formating) {
- std::map<fs::path, fs::path> mapByChange;
- for (auto& i : path) {
- fs::path originPath = i;
- TagLib::FileRef test(i.generic_wstring().c_str());
- if (!test.isNull()) {
- std::wstring extension = i.extension();
- i.remove_filename();
- i /= nameByTag(formating, test.tag());
- i += extension;
- }
- mapByChange.emplace(std::make_pair(originPath, i));
- }
- return mapByChange;
- }
- void renameAllFile(std::map<fs::path, fs::path> list) {
- for (auto& i : list) {
- if (i.first != i.second) {
- std::wcout << i.first << " TO : " << i.second << std::endl;
- if(!fs::exists(i.second))
- fs::rename(i.first, i.second);
- else {
- uint64_t number = 0;
- std::wstring extension = i.second.extension().wstring();
- fs::path bufI = i.second;
- while (fs::exists(bufI)) {
- bufI = i.second;
- bufI.replace_extension("");
- bufI += std::to_wstring(number);
- bufI += extension;
- number++;
- }
- std::wcout << i.first << " FINALY TO : " << bufI << std::endl;
- fs::rename(i.first, bufI);
- }
- }
- }
- }
- wchar_t* toWchar(char* string) {
- size_t size = strlen(string)+1;
- wchar_t* buf = new wchar_t[size];
- size_t tmp = 0;
- mbstowcs_s(&tmp, buf, size, string, size);
- return buf;
- }
- int main(int argc, char* argv[]) {
- if (argc == 2) {
- renameAllFile(formateAllFileByTag(getAllFileDirectory(".\\"), toWchar(argv[1])));
- }
- else {
- std::wcout << L"Please type : renamer.exe [your config here]\n%album%\n%title%\n%artist%\n%year%\n%track%" << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement