parent
a6c994fa65
commit
f99ca8bb26
@ -1,4 +1,4 @@
|
||||
public class Program {
|
||||
public class Program {
|
||||
static void Main(string[] args) {
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
@ -11,8 +11,6 @@ public class Program {
|
||||
return;
|
||||
}
|
||||
|
||||
Chksum.getBaseDir();
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
switch (args[0]) {
|
||||
case "checksum":
|
||||
@ -48,9 +46,6 @@ public class Program {
|
||||
|
||||
Chksum.compareChecksums();
|
||||
break;
|
||||
case "createDB":
|
||||
Chksum.initializeDB();
|
||||
break;
|
||||
case "help":
|
||||
PrintAvailableOptions();
|
||||
break;
|
||||
@ -68,7 +63,6 @@ public class Program {
|
||||
"countmd5",
|
||||
"deletemd5",
|
||||
"compareChecksums",
|
||||
"createDB",
|
||||
"help"
|
||||
};
|
||||
|
||||
|
49
chksum.cs
49
chksum.cs
@ -1,10 +1,7 @@
|
||||
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() {
|
||||
@ -22,30 +19,6 @@ 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)) {
|
||||
@ -63,24 +36,10 @@ public class Chksum {
|
||||
FileInfo[] files = dir.GetFiles();
|
||||
foreach (FileInfo file in files) {
|
||||
string fileName = file.Name;
|
||||
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();
|
||||
}
|
||||
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
|
||||
string checksumFile = Directory.GetCurrentDirectory() + "/" + fileNameWithoutExtension + ".md5";
|
||||
File.AppendAllText(checksumFile, CalculateMD5(fileName) + " " + fileName);
|
||||
Console.WriteLine(checksumFile);
|
||||
}
|
||||
}
|
||||
doTheThing();
|
||||
|
@ -11,8 +11,4 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="7.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Loading…
Reference in New Issue
Block a user