Major changes

Now using an SQlite database to store the file hash plus a bunch of other information
This commit is contained in:
ProfessionalUwU 2023-06-24 01:02:03 +02:00
parent 951068c750
commit 846b983caa
Signed by: ProfessionalUwU
GPG Key ID: 9F28CB1645C4BFB5
2 changed files with 52 additions and 5 deletions

View File

@ -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"
};

View File

@ -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();