// MyRename.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。 // #include "pch.h" #include #include // tsuji #include // tsuji #include // tsuji #include // tsuji #include // tsuji #include #include #include #include using namespace std; bool listFiles(_TCHAR *dir); //std::wstring last_date = L""; //int same_date = 1; std::vector > exist_date; /**************************************************************/ /// /// main() /// int atcg /// TCHAR* argv[] /// /// int 成功=0 int _tmain(int argc, TCHAR* argv[]) { _TCHAR lpFileName[MAX_PATH]; if (argc == 2) { _stprintf_s(lpFileName, _countof(lpFileName), TEXT("%s"), argv[1]); } else { _tcscpy_s(lpFileName, _countof(lpFileName), TEXT("")); } listFiles(lpFileName); return 0; } /**************************************************************/ /// /// FindDate /// bool FindDate(wstring filename) { //コンソール出力のロケール指定 std::wcout.imbue(std::locale("")); wifstream fileIn; fileIn.open(filename.c_str()); if (!fileIn.is_open()) { std::wcout << "ファイルをオープンできません" << endl; return false; } //入力ファイルをutf-8に設定 fileIn.imbue(std::locale(std::locale(""), new std::codecvt_utf8_utf16())); bool found = false; int found_rank = 100; std::wstring found_date; //ファイル読み込み while (1) { std::wstring w_str1; // 1行読み込みバッファ fileIn >> w_str1; if (fileIn.eof()) { break; } //指定した日付パターンに一致したら日付を取り出してループを抜ける typedef std::tuple mytuple; std::vector > date_mh; date_mh.push_back(mytuple(1, LR"(.*([0-9][0-9])年([01][0-9])月([0123][0-9])日.*)", L"$1$2$3")); date_mh.push_back(mytuple(2, LR"(.*([0-9][0-9])年([01][0-9])月([0-9])日.*)", L"$1$2_$3")); date_mh.push_back(mytuple(3, LR"(.*([0-9][0-9])/([01]*[0-9])/([0123][0-9]).*)", L"$1$2$3")); date_mh.push_back(mytuple(4, LR"(.*20([012][0-9])年.*([01][0-9])月.*([0123][0-9])日.*)", L"$1$2$3")); date_mh.push_back(mytuple(5, LR"(.*20([012][0-9]).*([01][0-9])月.*([0123][0-9])日.*)", L"$1$2$3")); for (int i = 0; i < date_mh.size(); i++) { if (regex_match(w_str1, get<1>(date_mh[i]))) { if (found_rank > get<0>(date_mh[i])) { found_rank = get<0>(date_mh[i]); found_date = regex_replace(w_str1, get<1>(date_mh[i]), get<2>(date_mh[i]), std::regex_constants::format_first_only); } found = true; //見つかった if (found_rank == 1) break; } } } if (found) { bool found_in_list = false; int date_count = 1; for (auto &i : exist_date) { if (i.first == found_date) { //前と同じ日付ならば、同じ日付の連番をinc i.second++; found_in_list = true; date_count = i.second; } } if (!found_in_list) { exist_date.push_back(make_pair(found_date, 1)); //前と違う日付ならば連番を1に設定 date_count = 1; } //読み込んだ文字(日付)をrenameコマンドにしてコンソール出力 wprintf(L"rename %s %s%s%s%d%s\n", filename.c_str(), L"r_20", found_date.c_str(), L"_", date_count, L".txt"); } if (!found) { // 日付データが見つからない場合はその旨出力する wprintf(L"Date for %s is not found.\n", filename.c_str()); } fileIn.close(); return true; } /**************************************************************/ /// /// listFiles() /// フォルダ内の、引数で指定したファイル名(ワイルドカード使用可)をリストする /// /// _TCHAR *FileName /// bool true: success bool listFiles(_TCHAR *FileName) { WIN32_FIND_DATA FindFileData; // ファイル情報受け取り用構造体 HANDLE hFind; // ファイルハンドル // ファイルのリスト取得は、FindFirstFile()→FindNext()で次々に取得する形式 hFind = ::FindFirstFile(FileName, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { // ファイルが見つからなければ終了 _tprintf(TEXT("FindFiristFile failed (%d)"), GetLastError()); } else { BOOL foundNext = true; // FindNextFile()で次のファイルが見つかったらtrue、見つからなかったらfalse do { // フォルダではない場合のみFindDate()を呼び出す if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { FindDate(FindFileData.cFileName); } foundNext = ::FindNextFile(hFind, &FindFileData); // 次のファイルを取得 } while (foundNext); // FindFristFile,FindNextFileで取得したファイルハンドルをclose ::FindClose(hFind); } return true; } // プログラムの実行: Ctrl + F5 または [デバッグ] > [デバッグなしで開始] メニュー // プログラムのデバッグ: F5 または [デバッグ] > [デバッグの開始] メニュー // 作業を開始するためのヒント: // 1. ソリューション エクスプローラー ウィンドウを使用してファイルを追加/管理します // 2. チーム エクスプローラー ウィンドウを使用してソース管理に接続します // 3. 出力ウィンドウを使用して、ビルド出力とその他のメッセージを表示します // 4. エラー一覧ウィンドウを使用してエラーを表示します // 5. [プロジェクト] > [新しい項目の追加] と移動して新しいコード ファイルを作成するか、[プロジェクト] > [既存の項目の追加] と移動して既存のコード ファイルをプロジェクトに追加します // 6. 後ほどこのプロジェクトを再び開く場合、[ファイル] > [開く] > [プロジェクト] と移動して .sln ファイルを選択します