From 846b983caab9089262600824b5b6c3094cc7fdb6 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Sat, 24 Jun 2023 01:02:03 +0200 Subject: [PATCH] Major changes Now using an SQlite database to store the file hash plus a bunch of other information --- Program.cs | 8 +++++++- chksum.cs | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/Program.cs b/Program.cs index d0873c6..4f3ba64 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,4 @@ -public class Program { +public class Program { static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Red; @@ -11,6 +11,8 @@ return; } + Chksum.getBaseDir(); + Console.ForegroundColor = ConsoleColor.Green; switch (args[0]) { case "checksum": @@ -46,6 +48,9 @@ Chksum.compareChecksums(); break; + case "createDB": + Chksum.initializeDB(); + break; case "help": PrintAvailableOptions(); break; @@ -63,6 +68,7 @@ "countmd5", "deletemd5", "compareChecksums", + "createDB", "help" }; diff --git a/chksum.cs b/chksum.cs index 06af796..7cb4265 100644 --- a/chksum.cs +++ b/chksum.cs @@ -1,7 +1,10 @@ +using Microsoft.Data.Sqlite; + // Go into folder // Check if any file is in there // If there is a file. Calculate md5sum > filename.md5 // If there is no file. Repeat + public class Chksum { // int getDirectoryCount() { @@ -19,6 +22,30 @@ public class Chksum { // return parentFolder; // } + public static string DatabaseRoot { get; set; } + public static void getBaseDir() { + DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory; + } + + public static void initializeDB() { + using (var connection = new SqliteConnection("Data Source=chksum.db")) { + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = + @" + CREATE TABLE file ( + filehash TEXT NOT NULL PRIMARY KEY, + filename TEXT NOT NULL, + pathtofile TEXT NOT NULL, + artist TEXT, + playbacklength INTEGER + ); + "; + command.ExecuteNonQuery(); + } + } + private static string CalculateMD5(string filename) { using (var md5 = System.Security.Cryptography.MD5.Create()) { using (var stream = File.OpenRead(filename)) { @@ -36,10 +63,24 @@ public class Chksum { FileInfo[] files = dir.GetFiles(); foreach (FileInfo file in files) { string fileName = file.Name; - string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); - string checksumFile = Directory.GetCurrentDirectory() + "/" + fileNameWithoutExtension + ".md5"; - File.AppendAllText(checksumFile, CalculateMD5(fileName) + " " + fileName); - Console.WriteLine(checksumFile); + string absolutePathToFile = Path.GetFullPath(fileName); + string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile); + string fileHash = CalculateMD5(fileName); + + using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) { + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = + @" + INSERT INTO file (filehash, filename, pathtofile) + VALUES ($filehash, $filename, $pathtofile) + "; + command.Parameters.AddWithValue("$filehash", fileHash); + command.Parameters.AddWithValue("$filename", fileName); + command.Parameters.AddWithValue("$pathtofile", pathToFile); + command.ExecuteNonQuery(); + } } } doTheThing();