feature/SQlite #1
@ -83,7 +83,7 @@ public class ChksumUtils {
|
|||||||
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
||||||
string fileHash = CalculateMD5(fileName);
|
string fileHash = CalculateMD5(fileName);
|
||||||
|
|
||||||
if (checkIfFileAlreadyExists(fileHash, fileName) == false) {
|
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExists(fileHash, fileName) == false) {
|
||||||
connection.Open();
|
connection.Open();
|
||||||
|
|
||||||
var command = connection.CreateCommand();
|
var command = connection.CreateCommand();
|
||||||
@ -103,9 +103,9 @@ public class ChksumUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool checkIfFileAlreadyExists(string fileHash, string fileName) {
|
private bool checkIfFileAlreadyExists(string fileHash, string pathToFile) {
|
||||||
string filehash = string.Empty;
|
string filehash = string.Empty;
|
||||||
string filename = string.Empty;
|
string pathtofile = string.Empty;
|
||||||
|
|
||||||
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
||||||
connection.Open();
|
connection.Open();
|
||||||
@ -113,26 +113,65 @@ public class ChksumUtils {
|
|||||||
var command = connection.CreateCommand();
|
var command = connection.CreateCommand();
|
||||||
command.CommandText =
|
command.CommandText =
|
||||||
@"
|
@"
|
||||||
SELECT filehash, filename FROM file WHERE filehash = $filehash
|
SELECT filehash, pathtofile FROM file WHERE filehash = $filehash
|
||||||
";
|
";
|
||||||
command.Parameters.AddWithValue("$filehash", fileHash);
|
command.Parameters.AddWithValue("$filehash", fileHash);
|
||||||
|
|
||||||
using (var reader = command.ExecuteReader()) {
|
using (var reader = command.ExecuteReader()) {
|
||||||
while (reader.Read()) {
|
while (reader.Read()) {
|
||||||
filehash = reader.GetString(0);
|
filehash = reader.GetString(0);
|
||||||
filename = reader.GetString(1);
|
pathtofile = reader.GetString(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fileHash == filehash) {
|
if (fileHash == filehash) {
|
||||||
Console.WriteLine($"Duplicate files found: {fileName} with the hash {fileHash} is identical to {filename} with the hash {filehash}");
|
Console.WriteLine($"Duplicate files found: {pathToFile} with the hash {fileHash} is identical to {pathtofile} with the hash {filehash}");
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool checkIfFileMovedAndUpdatePathToFile(string fileHash, string fileName, string pathToFile) {
|
||||||
|
string pathtofile = string.Empty;
|
||||||
|
|
||||||
|
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
var command = connection.CreateCommand();
|
||||||
|
command.CommandText =
|
||||||
|
@"
|
||||||
|
SELECT pathtofile FROM file WHERE filehash = $filehash
|
||||||
|
";
|
||||||
|
command.Parameters.AddWithValue("$filehash", fileHash);
|
||||||
|
|
||||||
|
using (var reader = command.ExecuteReader()) {
|
||||||
|
while (reader.Read()) {
|
||||||
|
pathtofile = reader.GetString(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pathToFile != pathtofile && pathtofile != "") {
|
||||||
|
var command2 = connection.CreateCommand();
|
||||||
|
command2.CommandText =
|
||||||
|
@"
|
||||||
|
UPDATE file
|
||||||
|
SET pathtofile = $newpathtofile
|
||||||
|
WHERE filehash = $filehash
|
||||||
|
";
|
||||||
|
command2.Parameters.AddWithValue("$newpathtofile", pathToFile);
|
||||||
|
command2.Parameters.AddWithValue("$filehash", fileHash);
|
||||||
|
command2.ExecuteNonQuery();
|
||||||
|
|
||||||
|
Console.WriteLine($"File moved: {fileName} was previously at {pathtofile} but is now at {pathToFile}");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private int getTotalFileCount() {
|
private int getTotalFileCount() {
|
||||||
int totalFileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories).Length;
|
int totalFileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories).Length;
|
||||||
return totalFileCount - 1; // Remove the program from the totalFileCount
|
return totalFileCount - 1; // Remove the program from the totalFileCount
|
||||||
|
Loading…
x
Reference in New Issue
Block a user