Compare commits
10 Commits
35f622ea01
...
afde0af0a4
Author | SHA1 | Date | |
---|---|---|---|
afde0af0a4 | |||
6f295b0e06 | |||
|
743e1eec20 | ||
8a8521b9b4 | |||
38d4330892 | |||
dfb7cd8405 | |||
454717431f | |||
2a8bf7e8fe | |||
23f8158ebf | |||
64d2400292 |
26
.editorconfig
Normal file
26
.editorconfig
Normal file
@ -0,0 +1,26 @@
|
||||
# Remove the line below if you want to inherit .editorconfig settings from higher directories
|
||||
root = true
|
||||
|
||||
# C# files
|
||||
[*.cs]
|
||||
|
||||
# Indentation and spacing
|
||||
indent_size = 4
|
||||
indent_style = space
|
||||
tab_width = 4
|
||||
|
||||
# Naming Conventions
|
||||
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
|
||||
|
||||
# New line preferences
|
||||
csharp_new_line_before_open_brace = none
|
||||
csharp_new_line_before_else = false
|
||||
csharp_new_line_before_catch = false
|
||||
csharp_new_line_before_finally = false
|
||||
|
||||
# Wrapping preferences
|
||||
csharp_preserve_single_line_statements = true
|
||||
csharp_preserve_single_line_blocks = true
|
||||
|
||||
# Switch case
|
||||
csharp_indent_case_contents = true
|
106
Program.cs
106
Program.cs
@ -1,50 +1,76 @@
|
||||
// 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 Program {
|
||||
public class Program {
|
||||
static void Main(string[] args) {
|
||||
|
||||
// int getDirectoryCount() {
|
||||
// int folderCount = Directory.GetDirectories(Directory.GetCurrentDirectory()).Length; // Get folder count in current directory
|
||||
// return folderCount;
|
||||
// }
|
||||
|
||||
int getFileCount() {
|
||||
int fileCount = Directory.GetFiles(Directory.GetCurrentDirectory()).Length; // Get file count in current directory
|
||||
return fileCount;
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
if (args.Length == 0) {
|
||||
Console.WriteLine("Please specify an option.");
|
||||
PrintAvailableOptions();
|
||||
return;
|
||||
} else if (args.Length > 1) {
|
||||
Console.WriteLine("Too many options.");
|
||||
return;
|
||||
}
|
||||
|
||||
// string getParentFolder() {
|
||||
// string parentFolder = Directory.GetParent(Directory.GetCurrentDirectory()).ToString(); // Get parent folder of current directory
|
||||
// return parentFolder;
|
||||
// }
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
switch (args[0]) {
|
||||
case "checksum":
|
||||
Console.WriteLine("Starting the checksum process.");
|
||||
Console.ResetColor();
|
||||
|
||||
string CalculateMD5(string filename) {
|
||||
using (var md5 = System.Security.Cryptography.MD5.Create())
|
||||
{
|
||||
using (var stream = File.OpenRead(filename))
|
||||
{
|
||||
var hash = md5.ComputeHash(stream);
|
||||
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
|
||||
}
|
||||
}
|
||||
Chksum.doTheThing();
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine("Checksum process finished");
|
||||
break;
|
||||
case "countmd5":
|
||||
Console.WriteLine("Counting md5 checksum files.");
|
||||
Console.ResetColor();
|
||||
|
||||
Chksum.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();
|
||||
|
||||
Chksum.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();
|
||||
|
||||
Chksum.compareChecksums();
|
||||
break;
|
||||
case "help":
|
||||
PrintAvailableOptions();
|
||||
break;
|
||||
default:
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("Invalid option. Maybe you mistyped it?");
|
||||
PrintAvailableOptions();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) {
|
||||
string parentFolder = directory;
|
||||
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.AppendAllText(checksumFile, CalculateMD5(fileName) + " " + fileName);
|
||||
}
|
||||
}
|
||||
Directory.SetCurrentDirectory(parentFolder); // Go back to the original root
|
||||
static void PrintAvailableOptions() {
|
||||
String[] options = {
|
||||
"checksum",
|
||||
"countmd5",
|
||||
"deletemd5",
|
||||
"compareChecksums",
|
||||
"help"
|
||||
};
|
||||
|
||||
Console.ResetColor();
|
||||
Console.WriteLine("usage: chksum [option]");
|
||||
Console.WriteLine("Here is a list of all available options:");
|
||||
foreach (String option in options) {
|
||||
Console.WriteLine("\t" + option);
|
||||
}
|
||||
}
|
||||
}
|
@ -25,13 +25,13 @@ pacman -S dotnet-runtime dotnet-sdk
|
||||
Build project
|
||||
|
||||
```bash
|
||||
dotnet build chksum.csproj
|
||||
dotnet build chksum.csproj
|
||||
```
|
||||
|
||||
Publish project
|
||||
|
||||
```bash
|
||||
dotnet publish --configuration Release --arch x64 --use-current-runtime --self-contained
|
||||
dotnet publish --configuration Release chksum.csproj
|
||||
```
|
||||
|
||||
Go to the publish folder
|
||||
|
111
chksum.cs
Normal file
111
chksum.cs
Normal file
@ -0,0 +1,111 @@
|
||||
// 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() {
|
||||
// int folderCount = Directory.GetDirectories(Directory.GetCurrentDirectory()).Length; // Get folder count in current directory
|
||||
// return folderCount;
|
||||
// }
|
||||
|
||||
private static 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;
|
||||
// }
|
||||
|
||||
private static string CalculateMD5(string filename) {
|
||||
using (var md5 = System.Security.Cryptography.MD5.Create()) {
|
||||
using (var stream = File.OpenRead(filename)) {
|
||||
var hash = md5.ComputeHash(stream);
|
||||
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void doTheThing() {
|
||||
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.AppendAllText(checksumFile, CalculateMD5(fileName) + " " + fileName);
|
||||
Console.WriteLine(checksumFile);
|
||||
}
|
||||
}
|
||||
doTheThing();
|
||||
}
|
||||
}
|
||||
|
||||
private static int getTotalFileCount() {
|
||||
int totalFileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories).Length;
|
||||
return totalFileCount - 1; // Remove the program from the totalFileCount
|
||||
}
|
||||
|
||||
public static void countAllMd5Checksums() {
|
||||
int totalMD5FileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.md5", SearchOption.AllDirectories).Length;
|
||||
Console.WriteLine("There are " + totalMD5FileCount + " md5 checksums");
|
||||
}
|
||||
|
||||
public static 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();
|
||||
}
|
||||
}
|
||||
|
||||
public static void compareChecksums() {
|
||||
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();
|
||||
// files.ToList().ForEach(i => Console.WriteLine(i.ToString())); // Print all files in files array
|
||||
foreach (FileInfo file in files) {
|
||||
string fileName = file.Name;
|
||||
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
|
||||
string checksumFile = Directory.GetCurrentDirectory() + "/" + fileNameWithoutExtension + ".md5";
|
||||
string fileMd5Checksum = fileNameWithoutExtension + ".md5";
|
||||
if (File.Exists(fileMd5Checksum)) {
|
||||
string newFileChecksum = CalculateMD5(fileName) + " " + fileName;
|
||||
string existingFileChecksum = File.ReadAllText(fileMd5Checksum);
|
||||
string newFileName = newFileChecksum.Substring(34);
|
||||
string existingFileName = existingFileChecksum.Substring(34);
|
||||
if (newFileChecksum.Equals(existingFileChecksum)) {
|
||||
Console.WriteLine(newFileName + " and " + existingFileName + " are the same.");
|
||||
} else {
|
||||
Console.WriteLine(newFileName + " and " + existingFileName + " are not the same.");
|
||||
Console.WriteLine("The checksum of " + newFileName + " is " + newFileChecksum);
|
||||
Console.WriteLine("The checksum of the already exting file " + existingFileName + " is " + existingFileChecksum);
|
||||
// TODO Tell the user to check which file is the correct one
|
||||
}
|
||||
} else {
|
||||
File.AppendAllText(checksumFile, CalculateMD5(fileName) + " " + fileName);
|
||||
Console.WriteLine("Calculated checksum for: " + checksumFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
compareChecksums();
|
||||
}
|
||||
}
|
||||
}
|
0
debugchksum.cs
Normal file
0
debugchksum.cs
Normal file
Loading…
x
Reference in New Issue
Block a user