From a3fd5b6c04f5231c1a01aa92dca0407fbdce9350 Mon Sep 17 00:00:00 2001 From: AdAstra-LD <76622070+AdAstra-LD@users.noreply.github.com> Date: Fri, 31 Dec 2021 20:48:57 +0100 Subject: [PATCH] Parallelized Content-based batch rename --- DS_Map/Main Window.cs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/DS_Map/Main Window.cs b/DS_Map/Main Window.cs index ecba121..5618d85 100644 --- a/DS_Map/Main Window.cs +++ b/DS_Map/Main Window.cs @@ -8769,15 +8769,15 @@ namespace DSPRE { "Do you want to proceed?", "Confirm operation", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr.Equals(DialogResult.Yes)) { - List enumerationFile = new List { + List enumeratedFiles = new List { "#============================================================================", "# File enumeration definition", "#============================================================================" }; - int initialLength = enumerationFile.Count; + int initialLength = enumeratedFiles.Count; const byte toRead = 16; - foreach (FileInfo f in files) { + System.Threading.Tasks.Parallel.ForEach(files, (f) => { Console.WriteLine(f.Name); string fileNameOnly = Path.GetFileNameWithoutExtension(f.FullName); @@ -8787,7 +8787,7 @@ namespace DSPRE { byte[] b = DSUtils.ReadFromFile(f.FullName, 0, toRead); if (b == null || b.Length < toRead) { - continue; + return; } string magic = ""; @@ -8800,9 +8800,9 @@ namespace DSPRE { destName = dirNameOnly + "\\"; //Full filename can be changed } - nameOffset = (ushort) ( 52 + (4 * (BitConverter.ToUInt16(b, 0xE) - 1)) ); + nameOffset = (ushort)(52 + (4 * (BitConverter.ToUInt16(b, 0xE) - 1))); destName += Encoding.UTF8.GetString(DSUtils.ReadFromFile(f.FullName, nameOffset, 16)).TrimEnd(new char[] { (char)0 }); - + destName += ".ns"; for (int i = 0; i < 3; i++) { magic += Char.ToLower((char)b[i]); @@ -8811,7 +8811,7 @@ namespace DSPRE { destName = fileNameOnly + "."; byte offset = 0; - if (b[5] == 'R' && b[8] == 'N') { + if (b[5] == 'R' && b[8] == 'N') { offset = 5; } @@ -8821,22 +8821,22 @@ namespace DSPRE { } if (string.IsNullOrWhiteSpace(magic) || !magic.All(char.IsLetterOrDigit)) { - continue; + return; } destName += magic; if (string.IsNullOrWhiteSpace(destName)) { - continue; + return; } destName = MakeUniqueName(destName, fileNameOnly = null, dirNameOnly); File.Move(f.FullName, destName); - enumerationFile.Add(Path.GetFileName(destName)); - } + enumeratedFiles.Add(Path.GetFileName(destName)); + }); - if (enumerationFile.Count > initialLength) { + if (enumeratedFiles.Count > initialLength) { MessageBox.Show("Files inside folder \"" + d.FullName + "\" have been renamed according to their contents.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult response = MessageBox.Show("Do you want to save a file enumeration list?", "Waiting for user", MessageBoxButtons.YesNo, MessageBoxIcon.Question); @@ -8852,7 +8852,7 @@ namespace DSPRE { return; } - File.WriteAllLines(sf.FileName, enumerationFile); + File.WriteAllLines(sf.FileName, enumeratedFiles); MessageBox.Show("List file saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else {