예제 #1
0
 public void organize()
 {
     deleteEmptyFolders(Properties.Settings.Default.MusicFilePath);
     new Thread(new ThreadStart(() => {
         try {
             string filesPath           = Properties.Settings.Default.MusicFilePath;
             int changes                = 0;
             IEnumerable <string> files = Directory.EnumerateFiles(filesPath, "*.mp3", SearchOption.AllDirectories);
             StringBuilder output       = new StringBuilder();
             int count = files.Count(), i = 0;
             foreach (string path in files)
             {
                 if (cancel)
                 {
                     break;
                 }
                 TagLib.File f = TagLib.File.Create(path);
                 string artist = "" + f.getArtist(), album = "" + f.Tag.Album;
                 if (artist == null || artist.Equals(string.Empty))
                 {
                     artist = "Unknown";
                 }
                 string filename = f.Tag.Title;
                 if (filename == null || filename.Equals(string.Empty))
                 {
                     filename = Path.GetFileName(path);
                 }
                 else
                 {
                     filename = filename + ".mp3";
                 }
                 if (filename.StartsWith("0"))
                 {
                     filename = filename.Substring(2).Trim();
                 }
                 string regexSearch = new string(Path.GetInvalidFileNameChars());
                 Regex r            = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
                 filename           = r.Replace(filename, "");
                 artist             = r.Replace(artist, "");
                 album    = r.Replace(album, "");
                 filename = filename.CleanFileName();
                 album    = album.CleanFolderName();
                 artist   = artist.CleanFolderName();
                 if (!filename.EndsWith(".mp3"))
                 {
                     filename = filename + ".mp3";
                 }
                 string newPath = Path.Combine(filesPath, artist, filename);
                 if (!string.IsNullOrEmpty(album))
                 {
                     newPath = Path.Combine(filesPath, artist, album, filename);
                 }
                 if (!Directory.Exists(Path.GetDirectoryName(newPath)))
                 {
                     Directory.CreateDirectory(Path.GetDirectoryName(newPath));
                 }
                 try {
                     if (!path.ToLower().Equals(newPath.ToLower()))
                     {
                         File.Move(path, newPath);
                         output.AppendFormat("\\{0}\\{1}\\{2}\n    {3}\n",
                                             artist, album, filename, path);
                         changes++;
                     }
                 } catch (Exception) {
                     //output.AppendLine(e.Message);
                 }
                 i++;
                 outputLabel.runOnUiThread(() => {
                     outputLabel.Text = string.Format("Finished {0} of {1} files\n   {2} changes", i, count, changes);
                     outputLabel.Invalidate();
                 });
             }
             string outputText = output.ToString();
             if (!outputText.Trim().Equals(string.Empty))
             {
                 if (outputText.Length <= 1000)
                 {
                     Toast.show(outputText);
                 }
                 else
                 {
                     Toast.show(string.Format("{0} changes made", changes));
                 }
             }
             deleteEmptyFolders(Properties.Settings.Default.MusicFilePath);
         } catch (Exception e) { this.runOnUiThread(() => { MessageBox.Show(e.Message); }); }
     })).Start();
 }