|
| 1 | +using System.Diagnostics; |
| 2 | +using System.IO; |
| 3 | +using System.Reflection; |
| 4 | + |
| 5 | +Console.WriteLine("Dat File Renamer v0.1"); |
| 6 | + |
| 7 | +// get all files in current exe folder |
| 8 | +var currFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); |
| 9 | +currFolder = "Q:\\Games\\Locomotion\\ttd russia"; |
| 10 | +var datFiles = Directory.GetFiles(currFolder) |
| 11 | + .Where(f => Path.GetExtension(f).ToLower() == ".dat"); |
| 12 | + |
| 13 | +var count = datFiles.Count(); |
| 14 | +Console.WriteLine($"Checking {count} files in {currFolder}"); |
| 15 | + |
| 16 | +var mismatching = new List<(string originalFile, string datName)>(); |
| 17 | + |
| 18 | +foreach (var datFile in datFiles) |
| 19 | +{ |
| 20 | + // read each files S5Header |
| 21 | + using var fileStream = new FileStream(datFile, FileMode.Open, FileAccess.Read, FileShare.None, 32, FileOptions.SequentialScan | FileOptions.Asynchronous); |
| 22 | + using var reader = new BinaryReader(fileStream); |
| 23 | + var data = reader.ReadBytes(0x10); |
| 24 | + |
| 25 | + var flags = BitConverter.ToUInt32(data[0..4]); |
| 26 | + var datName = System.Text.Encoding.ASCII.GetString(data[4..12]).Trim(); |
| 27 | + var checksum = BitConverter.ToUInt32(data[12..16]); |
| 28 | + |
| 29 | + var filename = Path.GetFileNameWithoutExtension(datFile).Trim(); |
| 30 | + |
| 31 | + if (filename != datName) |
| 32 | + { |
| 33 | + mismatching.Add((datFile, datName)); |
| 34 | + Console.WriteLine($"Mismatch: Filename=\"{filename}\" DatName=\"{datName}\" Checksum={checksum}"); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +Console.WriteLine($"Found {mismatching.Count} file names mismatching"); |
| 39 | +Console.WriteLine("Would you like to rename these? (Y)es (N)o (A)ll"); |
| 40 | +var input = Console.ReadLine(); |
| 41 | + |
| 42 | +var allAtOnce = string.Equals(input, "a", StringComparison.OrdinalIgnoreCase); |
| 43 | +if (string.Equals(input, "y", StringComparison.OrdinalIgnoreCase) || allAtOnce) |
| 44 | +{ |
| 45 | + foreach (var names in mismatching) |
| 46 | + { |
| 47 | + Console.WriteLine($"{names}"); |
| 48 | + var dir = Path.GetDirectoryName(names.originalFile); |
| 49 | + var newFilename = Path.Join(dir, names.datName + Path.GetExtension(names.originalFile)); |
| 50 | + Console.WriteLine($"New path is: {newFilename}"); |
| 51 | + |
| 52 | + try |
| 53 | + { |
| 54 | + if (allAtOnce) |
| 55 | + { |
| 56 | + File.Move(names.originalFile, newFilename); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + Console.WriteLine("Rename this file? (Y/N) (Ctrl+C to exit)"); |
| 61 | + var inputPerFile = Console.ReadLine(); |
| 62 | + if (string.Equals(inputPerFile, "y", StringComparison.OrdinalIgnoreCase)) |
| 63 | + { |
| 64 | + File.Move(names.originalFile, newFilename); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + catch (IOException ex) |
| 69 | + { |
| 70 | + Console.WriteLine(ex); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +else |
| 75 | +{ |
| 76 | + Console.WriteLine("Rename cancelled"); |
| 77 | +} |
| 78 | + |
| 79 | +Console.WriteLine("Press any key to exit"); |
| 80 | +Console.ReadLine(); |
0 commit comments