ファイル名置換プログラム PJ-25
int timeflag; int methodflag; int filenum; String^ folderName;// 選択されたフォルダのフルパス //char befnm[500]; //char aftnm[500]; array<System::String ^>^ befnm; // = gcnew array<System::String ^>(500); array<System::String ^>^ aftnm; // = gcnew array<System::String ^>(500); private: System::Void radioButton1_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { timeflag = 0; } private: System::Void rbttb2_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { timeflag = 1; } private: System::Void Bdirget_Click(System::Object^ sender, System::EventArgs^ e) { int i=0; array<System::String ^>^ filepaths = gcnew array<System::String ^>(500); befnm = gcnew array<System::String ^>(500); FolderBrowserDialog ^dlg=gcnew FolderBrowserDialog(); if(dlg->ShowDialog() == System::Windows::Forms::DialogResult::OK) { folderName = dlg->SelectedPath;//選択されたフォルダを設定 filepaths = System::IO::Directory::GetFiles( folderName );//ファイル名配列格納 for each (String^ filepath in filepaths) { befnm[i] = IO::Path::GetFileName (filepath); listkouhofile->Items->Insert(i,befnm[i]); i++; } filenum = i-1; } } private: System::Void Bfileget_Click(System::Object^ sender, System::EventArgs^ e) { array<System::String ^>^ filepaths = gcnew array<System::String ^>(500); array<System::String ^>^ dirpath = gcnew array<System::String ^>(500); int i=0; befnm = gcnew array<System::String ^>(500); OpenFileDialog ^ openFileDialog1 = gcnew OpenFileDialog(); openFileDialog1->InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"; openFileDialog1->Filter = "MP3 Files|*.mp3"; openFileDialog1->Title = "PJ25録音MP3ファイルを選択"; openFileDialog1->Multiselect = true; // Show the Dialog. if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) { //プロパティ filepaths = openFileDialog1->FileNames; for each (String^ filepath in filepaths) { //ディレクトリの取得 dirpath[i] = IO::Path::GetDirectoryName (filepath); befnm[i] = IO::Path::GetFileName (filepath); listkouhofile->Items->Insert(i,befnm[i]); i++; } filenum = i-1; folderName = dirpath[0]; //変換フォルダを格納 簡易版 } } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { rbttb1->Checked = true; rbfolder->Checked = true; } private: System::Void rbfolder_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { methodflag = 0; } private: System::Void rbfile_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { methodflag = 1; } private: System::Void Bclear_Click(System::Object^ sender, System::EventArgs^ e) { //すべてをクリア befnm = nullptr; aftnm = nullptr; listkouhofile->Items->Clear(); listhenkan->Items->Clear(); } private: System::Void Bnamech_Click(System::Object^ sender, System::EventArgs^ e) { //char szBuff; String^ programpath; String^ timetablefile; String^ timetableline; int year; String^ year2; int month; String^ month2; int day; String^ day2; int hour; String^ hour2; int minu; String^ minu2; String^ week; String^ weekd; array<System::String^,2>^ temptable = gcnew array<System::String^,2>(200,3); array<System::String ^>^ kouhotable = gcnew array<System::String ^>(4); int i=0; int j; int k; int l; aftnm = gcnew array<System::String ^>(500); //ファイル読み込み programpath = IO::Path::GetDirectoryName(Application::ExecutablePath); IO::Directory::SetCurrentDirectory(programpath); if (timeflag==0){ timetablefile = "gyoukaitimetable.txt"; } else if (timeflag==1){ timetablefile = "realtimetable.txt"; } //行1列を格納 array<String^>^ RowData = System::IO::File::ReadAllLines(timetablefile); //データ格納用配列 array<array<String^>^>^ timetable = gcnew array<array<String^>^>(RowData->Length); for (i = 0; i < RowData->Length; i++){ //行のデータをカンマで区切って配列に分割する。 timetable[i] = RowData[i]->Split(','); } //MessageBox::Show (timetable[0][0]); //カレントディレクトリを変換フォルダに IO::Directory::SetCurrentDirectory(folderName); //録音ファイル名をひとつずつ分解して組みなおす for (i = 0; i <= filenum ; i++){ Int32::TryParse(befnm[i]->Substring(2, 2),l); year =2000 + l; Int32::TryParse(befnm[i]->Substring(4, 2),month); Int32::TryParse(befnm[i]->Substring(6, 2),day); Int32::TryParse(befnm[i]->Substring(11, 2),hour); Int32::TryParse(befnm[i]->Substring(13, 2),minu); //ファイル変換 if(timeflag==0){ if(hour < 5 && day >1){ //深夜録音のとき、前日に戻す day = day -1; hour = hour +24; } else if(hour < 5 && day == 1 && month != 1){ //月末録音深夜で次の月になる場合、前月に戻す month = month -1; day = DateTime::DaysInMonth(year,month); hour = hour +24; } else if(hour <5 && day==1 && month ==1){ //1/1 5時前の時は何もしない。 } } else if(timeflag==1){ } //曜日解析 weekd = Convert::ToString(DateTime(year,month,day).DayOfWeek); week = weekd->Substring(0, 3)->ToLower(); //ファイル名調整 year2 =befnm[i]->Substring(2, 2); if(month<10){ month2 = "0" + Convert::ToString(month); } else{ month2 = Convert::ToString(month); } if(day<10){ day2 = "0" + Convert::ToString(day); } else{ day2 = Convert::ToString(day); } if(hour<10){ hour2 = "0" + Convert::ToString(hour); } else{ hour2 = Convert::ToString(hour); } if(minu<10){ minu2 = "0" + Convert::ToString(minu); } else{ minu2 = Convert::ToString(minu); } temptable[i,0] = year2 + month2 + day2; temptable[i,1] = hour2 + minu2; temptable[i,2] = week; //タイムテーブルに登録されているか検索 for (j = 0; j < RowData->Length; j++){ // if (timetable[j][1]==temptable[i,1] && timetable[j][2]==temptable[i,2]){ kouhotable[0] = timetable[j][0]; //名前 kouhotable[1] = temptable[i,1]; //時刻 kouhotable[2] = temptable[i,2]; //曜日 kouhotable[3] = timetable[j][3]; //ファイル名形式 } } //置換後ファイル名決定 if (kouhotable[0] != nullptr && kouhotable[3] == "0"){ //番組名+年月日+曜日 aftnm[i] = kouhotable[0] + " " + temptable[i,0] + " " + kouhotable[2]; } else if (kouhotable[0] != nullptr && kouhotable[3] == "1"){ //番組名+年月日 aftnm[i] = kouhotable[0] + " " + temptable[i,0]; } else{ //時刻+年月日+曜日 aftnm[i] = temptable[i,1] + " " + temptable[i,0] + " " + temptable[i,2]; } //候補データを空にする kouhotable[0] = nullptr; //名前 kouhotable[1] = nullptr; //時刻 kouhotable[2] = nullptr; //曜日 kouhotable[3] = nullptr; //ファイル名形式 //重複ファイルチェック String^ tempname1; String^ tempname2; String^ renbankijun; int lennow; int lentemp; if (i != 0){ lennow = aftnm[i]->Length; tempname1 = aftnm[i]; tempname2 = aftnm[i]; for (j = 0; j < i ; j++){ lentemp = aftnm[j]->Length; if (lennow <= lentemp){ renbankijun = aftnm[j]->Substring(0,lennow); if(tempname1 == renbankijun){ if(lennow != lentemp){ k=0; int rhanbetu =0; while( rhanbetu <1){ k++; tempname2 = tempname1 + " " + k; if (tempname2 == aftnm[j]){ rhanbetu =1; } //2回目以降の重複回避 } tempname2 = tempname1 + " " + Convert::ToString(k+1); } else{ tempname2 = aftnm[i] + " 2"; aftnm[j] = aftnm[j] + " 1"; //最初の重複回避 } } } } aftnm[i] = tempname2; } }//ここまで組みなおしforループ for (i = 0; i <= filenum ; i++){ listhenkan->Items->Insert(i,befnm[i] + " -----> " + aftnm[i] + ".mp3"); } } private: System::Void Brename_Click(System::Object^ sender, System::EventArgs^ e) { //ファイル名変更 int i; IO::Directory::SetCurrentDirectory(folderName); for (i = 0; i <= filenum; i++){ IO::File::Move(befnm[i], aftnm[i] + ".mp3"); } MessageBox::Show ("変換完了"); } |