public void CleanUpManifestFile() { // read file string mp3ManifestFile = GetMp3ListFilePath(); List <string> lines = File.ReadAllText(mp3ManifestFile).Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList(); string pattern = @"(.+) \| (.+)"; string parseMp3(string line) => MatchGroup(line, pattern, 1); // filter out lines whose mp3s no longer exist List <string> keepLines = new List <string>(); foreach (string line in lines) { var relMp3 = parseMp3(line); var absMp3 = JunUtils.FullPathFromSongsFolder(relMp3); if (File.Exists(absMp3)) { keepLines.Add(line); } } // write to file File.WriteAllText(mp3ManifestFile, String.Join(Environment.NewLine, keepLines)); }
public DeleteMp3sForm(List <string> filesToDelete) { InitializeComponent(); List <string> uniqueFilesToDelete = filesToDelete.Distinct().ToList(); // filter out files that don't exist Dictionary <string, string> pathMapping = new Dictionary <string, string>(); uniqueFilesToDelete.ForEach(file => pathMapping.Add(file, JunUtils.FullPathFromSongsFolder(file))); List <string> relativeFileList = uniqueFilesToDelete.Where(file => File.Exists(JunUtils.FullPathFromSongsFolder(file))).ToList(); // populate listview string formatFileSize(long len) { string[] sizes = { "B", "KB", "MB", "GB", "TB" }; int order = 0; decimal value = (decimal)len; while (value >= 1024 && order < sizes.Length - 1) { order++; value = value / 1024; } return(String.Format("{0:0.##} {1}", value, sizes[order])); } ListViewItem fileToListViewItem(string file) { string[] subitems = new string[3]; FileInfo fi = new FileInfo(JunUtils.FullPathFromSongsFolder(file)); subitems[0] = file; // path relative to songs folder subitems[1] = fi.CreationTime.ToString("d"); // date created subitems[2] = formatFileSize(fi.Length); // size return(new ListViewItem(subitems)); } fileListView.Items.Clear(); relativeFileList .Select(file => fileToListViewItem(file)) .ToList() .ForEach(item => fileListView.Items.Add(item)); // update total filesize label long totalSize = relativeFileList .Select(file => new FileInfo(JunUtils.FullPathFromSongsFolder(file)).Length) .Sum(); fileSizeLabel.Text = $"Total: {formatFileSize(totalSize)} to be deleted"; confirmButton.Focus(); }
public List <string> GetUnusedMp3s() { // read manifest file List <string> lines = new List <string>(); string mp3ManifestFile = GetMp3ListFilePath(); if (!File.Exists(mp3ManifestFile)) { return(new List <string>()); } using (var reader = File.OpenText(mp3ManifestFile)) { string line = ""; while ((line = reader.ReadLine()) != null) { lines.Add(line); } } // convert that shit into a dictionary var mp3Dict = new Dictionary <string, List <string> >(); string pattern = @"(.+) \| (.+)"; string parseMp3(string line) => MatchGroup(line, pattern, 1); string parseOsu(string line) => MatchGroup(line, pattern, 2); // create dictionary keys lines .Select(line => parseMp3(line)).ToList() .Distinct() .ToList() .ForEach(mp3 => mp3Dict.Add(mp3, new List <string>())); // populate dictionary values foreach ((string mp3, string osu) in lines.Select(line => (parseMp3(line), parseOsu(line)))) { mp3Dict[mp3].Add(osu); } // find all keys where none of the associated beatmaps exist, but the mp3 still exists bool noFilesExist(bool acc, string file) => acc && !File.Exists(file); return(lines .Select(line => parseMp3(line)) .Where(mp3 => mp3Dict[mp3].Aggregate(true, noFilesExist)) .Where(mp3 => File.Exists(JunUtils.FullPathFromSongsFolder(mp3))) .ToList()); }
private void DeleteButton_Click(object sender, EventArgs e) { var mp3List = editor.GetUnusedMp3s(); if (new DeleteMp3sForm(mp3List).ShowDialog() == DialogResult.OK) { mp3List .Select(relativeMp3 => JunUtils.FullPathFromSongsFolder(relativeMp3)) .Select(absMp3 => new FileInfo(absMp3)) .ToList() .ForEach(file => file.Delete()); if (mp3List.Count > 0) { MessageBox.Show($"Deleted {mp3List.Count} file(s).", "Success"); } editor.CleanUpManifestFile(); } }
private void DeleteButton_Click(object sender, EventArgs e) { if (gameLoaded == true) { MessageBox.Show("Please close osu! first then try again.", "osu! is running"); return; } var mp3List = editor.GetUnusedMp3s(); if (new DeleteMp3sForm(mp3List).ShowDialog() == DialogResult.OK) { mp3List .Select(relativeMp3 => JunUtils.FullPathFromSongsFolder(relativeMp3)) .Select(absMp3 => new FileInfo(absMp3)) .ToList() .ForEach(file => file.Delete()); if (mp3List.Count > 0) { MessageBox.Show($"Deleted {mp3List.Count} file(s).", "Success"); } editor.CleanUpManifestFile(); } }