Add option to check for deleted files

This commit is contained in:
ProfessionalUwU 2023-06-26 15:59:06 +02:00
parent 2d42842fb2
commit b899c4c5b6
Signed by: ProfessionalUwU
GPG Key ID: 9F28CB1645C4BFB5
2 changed files with 39 additions and 0 deletions

View File

@ -39,6 +39,10 @@ public class Program {
case "createDB":
utils.initializeDB();
break;
case "checkIfFileWasDeleted":
Console.ResetColor();
utils.checkIfFileWasDeleted();
break;
case "help":
PrintAvailableOptions();
break;
@ -57,6 +61,7 @@ public class Program {
"checksum",
"compareChecksums",
"createDB",
"checkIfFileWasDeleted",
"help"
};

View File

@ -174,6 +174,40 @@ public class ChksumUtils {
}
}
public void checkIfFileWasDeleted() {
string pathToFile = string.Empty;
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
connection.Open();
var selectCommand = connection.CreateCommand();
selectCommand.CommandText =
@"
Select pathtofile FROM file
";
using (var reader = selectCommand.ExecuteReader()) {
while (reader.Read()) {
pathToFile = reader.GetString(0);
if (!File.Exists(pathToFile)) {
var deleteCommand = connection.CreateCommand();
deleteCommand.CommandText =
@"
DELETE FROM file
WHERE pathtofile = $pathtofile
";
deleteCommand.Parameters.AddWithValue("$pathtofile", pathToFile);
deleteCommand.ExecuteNonQuery();
Console.WriteLine("File deleted:");
Console.WriteLine($"\t{pathToFile}\n");
}
}
}
}
}
public void compareChecksums() { // reuse for database comparison
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) {
Directory.SetCurrentDirectory(directory); // Set new root