Skip to content

Commit

Permalink
Refactor file-moving logic and add executable check
Browse files Browse the repository at this point in the history
Updated the file-moving logic in Program.cs to use a while loop instead of a do-while loop for improved clarity. Introduced a new method, IsCurrentExecutable, that prevents the moving of the current executable file and simplifies related conditions. Improved logging by adding a specific log message if the current executable file is encountered.
  • Loading branch information
HakuSystems committed Oct 12, 2024
1 parent e98b57b commit f0f3856
Showing 1 changed file with 41 additions and 26 deletions.
67 changes: 41 additions & 26 deletions ServerFileManagerConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Reflection;
using System.Diagnostics;

namespace ServerFileManagerConsole;

Expand All @@ -22,54 +22,61 @@ private static void MoveFiles()
{
var folders = Enum.GetValues(typeof(FolderNames)).Cast<FolderNames>().ToList();
var currentDirectory = Directory.GetCurrentDirectory();
var currentExecutable = Assembly.GetExecutingAssembly().Location;
var currentExecutableName = Path.GetFileName(currentExecutable);

var fileEndingsList = folders.Select(folder => new FileEndings(folder)).ToList();

var allFiles = Directory.GetFiles(currentDirectory)
.Where(f => Path.GetFileName(f) != currentExecutableName)
.Where(f => !IsCurrentExecutable(f))
.ToList();

var totalFiles = allFiles.Count;
var processedFiles = 0;

do
{
var filesMovedInIteration = 0;

while (allFiles.Count > 0)
foreach (var file in allFiles.ToList())
if (!folders.Any(folder => file.StartsWith(Path.Combine(currentDirectory, folder.ToString()))))
{
if (folders.Any(folder => file.StartsWith(Path.Combine(currentDirectory, folder.ToString()))))
{
processedFiles++;
var percentage = processedFiles * 100 / totalFiles;
AnimateTitle($"Moving files... {percentage}%");
allFiles.Remove(file);
continue;
}

var fileMoved = false;
processedFiles++;
var percentage = processedFiles * 100 / totalFiles;
AnimateTitle($"Moving files... {percentage}%");

foreach (var fileEndings in fileEndingsList.Where(fileEndings => fileEndings.endings.Any(ending =>
file.EndsWith(ending, StringComparison.OrdinalIgnoreCase) ||
file.EndsWith("." + ending, StringComparison.OrdinalIgnoreCase))))
var fileMoved = false;

foreach (var fileEndings in fileEndingsList)
if (fileEndings.endings.Any(ending =>
file.EndsWith(ending, StringComparison.OrdinalIgnoreCase) ||
file.EndsWith("." + ending, StringComparison.OrdinalIgnoreCase)))
{
MoveFileToFolder(file, fileEndings.folder.ToString());
fileMoved = true;
filesMovedInIteration++;
allFiles.Remove(file);
break;
}

if (!fileMoved)
{
MoveFileToFolder(file, FolderNames.General.ToString());
filesMovedInIteration++;
allFiles.Remove(file);
}
if (!fileMoved)
{
MoveFileToFolder(file, FolderNames.General.ToString());
allFiles.Remove(file);
}
} while (allFiles.Count > 0);
}

SLogger.Log("All files have been moved to their respective folders.");
}

private static bool IsCurrentExecutable(string filePath)
{
var currentExecutablePath = Process.GetCurrentProcess().MainModule.FileName;
return string.Equals(
Path.GetFullPath(filePath),
Path.GetFullPath(currentExecutablePath),
StringComparison.OrdinalIgnoreCase);
}

private static void AnimateTitle(string title)
{
Console.Title = title;
Expand All @@ -79,10 +86,17 @@ private static void MoveFileToFolder(string sourceFilePath, string destinationFo
{
try
{
if (IsCurrentExecutable(sourceFilePath))
{
SLogger.Log("This executable file will not be moved.");
return;
}

var fileName = Path.GetFileName(sourceFilePath);
var destinationPath = Path.Combine(destinationFolder, fileName);

if (!Directory.Exists(destinationFolder)) Directory.CreateDirectory(destinationFolder);
if (!Directory.Exists(destinationFolder))
Directory.CreateDirectory(destinationFolder);

if (File.Exists(destinationPath))
File.Delete(destinationPath);
Expand All @@ -97,7 +111,8 @@ private static void MoveFileToFolder(string sourceFilePath, string destinationFo

private static void CreateAllFolders()
{
foreach (FolderNames folder in Enum.GetValues(typeof(FolderNames))) CreateFolder(folder);
foreach (FolderNames folder in Enum.GetValues(typeof(FolderNames)))
CreateFolder(folder);
}

private static void CreateFolder(FolderNames folder)
Expand Down

0 comments on commit f0f3856

Please sign in to comment.