code cleanup
removed unused methods changed output of methods
This commit is contained in:
parent
c51e02fa05
commit
8aec649781
@ -30,24 +30,6 @@ public class Program {
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine("Checksum process finished");
|
||||
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":
|
||||
Console.WriteLine("Comparing all md5 checksum files. If there is none, creating one.");
|
||||
Console.ResetColor();
|
||||
@ -73,16 +55,13 @@ public class Program {
|
||||
static void PrintAvailableOptions() {
|
||||
String[] options = {
|
||||
"checksum",
|
||||
"countmd5",
|
||||
"deletemd5",
|
||||
"compareChecksums",
|
||||
"createDB",
|
||||
"help"
|
||||
};
|
||||
|
||||
Console.ResetColor();
|
||||
Console.WriteLine("usage: chksum [option]");
|
||||
Console.WriteLine("Here is a list of all available options:");
|
||||
Console.WriteLine("usage: chksum [option] \nHere is a list of all available options:");
|
||||
foreach (String option in options) {
|
||||
Console.WriteLine("\t" + option);
|
||||
}
|
||||
|
@ -3,21 +3,11 @@ using Microsoft.Data.Sqlite;
|
||||
namespace Chksum.Utils;
|
||||
public class ChksumUtils {
|
||||
|
||||
// int getDirectoryCount() {
|
||||
// int folderCount = Directory.GetDirectories(Directory.GetCurrentDirectory()).Length; // Get folder count in current directory
|
||||
// return folderCount;
|
||||
// }
|
||||
|
||||
private int getFileCount() {
|
||||
int fileCount = Directory.GetFiles(Directory.GetCurrentDirectory()).Length; // Get file count in current directory
|
||||
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 void getBaseDir() {
|
||||
DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory;
|
||||
@ -38,12 +28,11 @@ public class ChksumUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
File.Delete(libraryPath);
|
||||
public void initializeDB() {
|
||||
if (File.Exists("chksum.db")) {
|
||||
return;
|
||||
}
|
||||
|
||||
public void initializeDB() {
|
||||
if (!File.Exists("chksum.db")) {
|
||||
using (var connection = new SqliteConnection("Data Source=chksum.db")) {
|
||||
connection.Open();
|
||||
|
||||
@ -61,7 +50,6 @@ public class ChksumUtils {
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanDB() {
|
||||
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db")) {
|
||||
@ -84,7 +72,8 @@ public class ChksumUtils {
|
||||
}
|
||||
|
||||
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
|
||||
if (getFileCount() >= 1) {
|
||||
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
||||
@ -118,6 +107,7 @@ public class ChksumUtils {
|
||||
private bool checkIfFileAlreadyExists(string fileHash, string pathToFile) {
|
||||
string filehash = string.Empty;
|
||||
string pathtofile = string.Empty;
|
||||
bool doesExist = false;
|
||||
|
||||
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
||||
connection.Open();
|
||||
@ -138,15 +128,17 @@ public class ChksumUtils {
|
||||
}
|
||||
|
||||
if (fileHash == filehash) {
|
||||
Console.WriteLine($"Duplicate files found: {pathToFile} with the hash {fileHash} is identical to {pathtofile} with the hash {filehash}");
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
Console.WriteLine("Duplicate files found:");
|
||||
Console.WriteLine($"\toriginal\t{pathToFile}");
|
||||
Console.WriteLine($"\tduplicate\t{pathtofile}\n");
|
||||
doesExist = true;
|
||||
}
|
||||
return doesExist;
|
||||
}
|
||||
|
||||
private bool checkIfFileMovedAndUpdatePathToFile(string fileHash, string fileName, string pathToFile) {
|
||||
string pathtofile = string.Empty;
|
||||
bool wasMoved = false;
|
||||
|
||||
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
||||
connection.Open();
|
||||
@ -176,39 +168,12 @@ public class ChksumUtils {
|
||||
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;
|
||||
Console.WriteLine("File moved:");
|
||||
Console.WriteLine($"\tfrom\t{pathToFile}");
|
||||
Console.WriteLine($"\tto \t{pathtofile}\n");
|
||||
wasMoved = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
return wasMoved;
|
||||
}
|
||||
}
|
||||
|
||||
@ -246,4 +211,8 @@ public class ChksumUtils {
|
||||
compareChecksums();
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
File.Delete(libraryPath);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user