Skip to content

Instantly share code, notes, and snippets.

@bizouarn
Last active September 3, 2024 12:00
Show Gist options
  • Save bizouarn/2497236e754a9e325e5e7a5968bd0b40 to your computer and use it in GitHub Desktop.
Save bizouarn/2497236e754a9e325e5e7a5968bd0b40 to your computer and use it in GitHub Desktop.
Divise un dump MySQL en fichiers distincts basés sur les commentaires pour en simplifier l'analyse.
using System.Text.RegularExpressions;
const string path = "Dumps.sql";
var title = "";
using var sr = new StreamReader(path);
StreamWriter sw = null;
try
{
while (sr.ReadLine() is { } line)
{
if (string.IsNullOrWhiteSpace(line) || line == "--")
continue;
if (line.StartsWith("--"))
{
// Nettoyage du titre
title = Regex.Replace(line.Trim('-', ' '), "[^a-zA-Z0-9]", "_") ;
if (string.IsNullOrEmpty(title))
continue;
title += ".sql";
while (File.Exists(title))
{
File.Delete(title);
}
sw?.Dispose(); // Fermer le StreamWriter précédent, si il existe
sw = new StreamWriter(title);
}
if (!string.IsNullOrEmpty(title))
{
sw.WriteLine(line);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
finally
{
sw?.Dispose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment