code cleanup

removed unused methods
changed output of methods
This commit is contained in:
AustrianToast 2023-06-25 22:37:50 +02:00
parent c51e02fa05
commit 8aec649781
No known key found for this signature in database
GPG Key ID: 5CD422268E489EB4
2 changed files with 37 additions and 89 deletions

View File

@ -30,24 +30,6 @@ public class Program {
Console.ForegroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Checksum process finished"); Console.WriteLine("Checksum process finished");
break; break;
case "countmd5":
Console.WriteLine("Counting md5 checksum files.");
Console.ResetColor();
utils.countAllMd5Checksums();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Finished counting all md5 checksum files.");
break;
case "deletemd5":
Console.WriteLine("Deleting all md5 checksum files.");
Console.ResetColor();
utils.deleteAllMd5Checksums();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Deleted all md5 checksum files.");
break;
case "compareChecksums": case "compareChecksums":
Console.WriteLine("Comparing all md5 checksum files. If there is none, creating one."); Console.WriteLine("Comparing all md5 checksum files. If there is none, creating one.");
Console.ResetColor(); Console.ResetColor();
@ -73,16 +55,13 @@ public class Program {
static void PrintAvailableOptions() { static void PrintAvailableOptions() {
String[] options = { String[] options = {
"checksum", "checksum",
"countmd5",
"deletemd5",
"compareChecksums", "compareChecksums",
"createDB", "createDB",
"help" "help"
}; };
Console.ResetColor(); Console.ResetColor();
Console.WriteLine("usage: chksum [option]"); Console.WriteLine("usage: chksum [option] \nHere is a list of all available options:");
Console.WriteLine("Here is a list of all available options:");
foreach (String option in options) { foreach (String option in options) {
Console.WriteLine("\t" + option); Console.WriteLine("\t" + option);
} }

View File

@ -3,21 +3,11 @@ using Microsoft.Data.Sqlite;
namespace Chksum.Utils; namespace Chksum.Utils;
public class ChksumUtils { public class ChksumUtils {
// int getDirectoryCount() {
// int folderCount = Directory.GetDirectories(Directory.GetCurrentDirectory()).Length; // Get folder count in current directory
// return folderCount;
// }
private int getFileCount() { private int getFileCount() {
int fileCount = Directory.GetFiles(Directory.GetCurrentDirectory()).Length; // Get file count in current directory int fileCount = Directory.GetFiles(Directory.GetCurrentDirectory()).Length; // Get file count in current directory
return fileCount; return fileCount;
} }
// string getParentFolder() {
// string parentFolder = Directory.GetParent(Directory.GetCurrentDirectory()).ToString(); // Get parent folder of current directory
// return parentFolder;
// }
public string DatabaseRoot { get; set; } = string.Empty; public string DatabaseRoot { get; set; } = string.Empty;
public void getBaseDir() { public void getBaseDir() {
DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory; DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory;
@ -38,28 +28,26 @@ public class ChksumUtils {
} }
} }
public void cleanup() {
File.Delete(libraryPath);
}
public void initializeDB() { public void initializeDB() {
if (!File.Exists("chksum.db")) { if (File.Exists("chksum.db")) {
using (var connection = new SqliteConnection("Data Source=chksum.db")) { return;
connection.Open(); }
var command = connection.CreateCommand(); using (var connection = new SqliteConnection("Data Source=chksum.db")) {
command.CommandText = connection.Open();
@"
CREATE TABLE file ( var command = connection.CreateCommand();
filehash TEXT NOT NULL PRIMARY KEY, command.CommandText =
filename TEXT NOT NULL, @"
pathtofile TEXT NOT NULL, CREATE TABLE file (
artist TEXT, filehash TEXT NOT NULL PRIMARY KEY,
playbacklength INTEGER filename TEXT NOT NULL,
); pathtofile TEXT NOT NULL,
"; artist TEXT,
command.ExecuteNonQuery(); playbacklength INTEGER
} );
";
command.ExecuteNonQuery();
} }
} }
@ -84,7 +72,8 @@ public class ChksumUtils {
} }
public void doTheThing() { public void doTheThing() {
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) { foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory()))
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
Directory.SetCurrentDirectory(directory); // Set new root Directory.SetCurrentDirectory(directory); // Set new root
if (getFileCount() >= 1) { if (getFileCount() >= 1) {
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory()); DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
@ -118,6 +107,7 @@ public class ChksumUtils {
private bool checkIfFileAlreadyExists(string fileHash, string pathToFile) { private bool checkIfFileAlreadyExists(string fileHash, string pathToFile) {
string filehash = string.Empty; string filehash = string.Empty;
string pathtofile = string.Empty; string pathtofile = string.Empty;
bool doesExist = false;
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();
@ -138,15 +128,17 @@ public class ChksumUtils {
} }
if (fileHash == filehash) { if (fileHash == filehash) {
Console.WriteLine($"Duplicate files found: {pathToFile} with the hash {fileHash} is identical to {pathtofile} with the hash {filehash}"); Console.WriteLine("Duplicate files found:");
return true; Console.WriteLine($"\toriginal\t{pathToFile}");
} else { Console.WriteLine($"\tduplicate\t{pathtofile}\n");
return false; doesExist = true;
} }
return doesExist;
} }
private bool checkIfFileMovedAndUpdatePathToFile(string fileHash, string fileName, string pathToFile) { private bool checkIfFileMovedAndUpdatePathToFile(string fileHash, string fileName, string pathToFile) {
string pathtofile = string.Empty; string pathtofile = string.Empty;
bool wasMoved = false;
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();
@ -176,39 +168,12 @@ public class ChksumUtils {
command2.Parameters.AddWithValue("$filehash", fileHash); command2.Parameters.AddWithValue("$filehash", fileHash);
command2.ExecuteNonQuery(); command2.ExecuteNonQuery();
Console.WriteLine($"File moved: {fileName} was previously at {pathtofile} but is now at {pathToFile}"); Console.WriteLine("File moved:");
return true; Console.WriteLine($"\tfrom\t{pathToFile}");
} else { Console.WriteLine($"\tto \t{pathtofile}\n");
return false; wasMoved = true;
} }
} return wasMoved;
}
private int getTotalFileCount() {
int totalFileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories).Length;
return totalFileCount - 1; // Remove the program from the totalFileCount
}
public void countAllMd5Checksums() {
int totalMD5FileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.md5", SearchOption.AllDirectories).Length;
Console.WriteLine("There are " + totalMD5FileCount + " md5 checksums");
}
public void deleteAllMd5Checksums() {
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) {
Directory.SetCurrentDirectory(directory); // Set new root
if (getFileCount() >= 1) {
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
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.Delete(checksumFile);
Console.WriteLine("Deleted " + checksumFile);
}
}
deleteAllMd5Checksums();
} }
} }
@ -246,4 +211,8 @@ public class ChksumUtils {
compareChecksums(); compareChecksums();
} }
} }
public void cleanup() {
File.Delete(libraryPath);
}
} }