public static void Guidfy(ProgramArguments arguments) { Log("Acquiring file names..", arguments); var isDirectory = Directory.Exists(arguments.Path); var isFile = File.Exists(arguments.Path); var files = isDirectory ? Directory.GetFiles(arguments.Path, "*", arguments.Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly) : new string[] { arguments.Path }; var basePath = isDirectory ? arguments.Path : Path.GetDirectoryName(arguments.Path); Log("Creating database..", arguments); var database = new Dictionary <string, string>(); Log("Generating GUIDs..", arguments); foreach (var file in files) { if (Path.GetFileName(file) == "database.json") { continue; } var guid = NextGuid; var relativePath = Path.GetRelativePath(basePath, file); Log($"\t{relativePath} → {guid}", arguments); if (database.ContainsKey(guid)) { throw new Exception($"GUID collision for file '{relativePath}'."); } database[guid] = file; } Log("Writing database to disk..", arguments); var jsonPath = Path.Combine(basePath, "database.json"); if (File.Exists(jsonPath) && !arguments.Force) { throw new Exception("Previous database already exists. Run with -f (--force) to overwrite (this WILL cause data loss)."); } var json = JsonSerializer.Serialize(database, new JsonSerializerOptions() { WriteIndented = true }); using (StreamWriter writer = new StreamWriter(jsonPath)) { writer.Write(json); writer.Flush(); } Log("Renaming files..", arguments); foreach (var file in database) { var extension = Path.GetExtension(file.Value); var directory = Path.GetDirectoryName(file.Value); File.Move(file.Value, Path.Combine(directory, $"{file.Key}{extension}")); } Log("Done!", arguments); }