hello kitty

This commit is contained in:
Elias Quinn
2025-06-09 16:14:20 +01:00
parent ff158be0c0
commit 467d6af393
70 changed files with 2020 additions and 149 deletions

View File

@@ -8,18 +8,23 @@ namespace file_finder__test;
public class FileClassifier
{
public async Task<(List<string> musicFiles, List<string> videoFiles)> ClassifyFilesAsync(
//returns 2 seperat lists music and vidio
public async Task<(List<string> musicFiles, List<string> videoFiles, List<string> photoFiles)> ClassifyFilesAsync(
//3 lists. all file paths and a list for vidio and music ext
List<string> allFiles,
List<string> musicExtensions,
List<string> videoExtensions)
List<string> videoExtensions,
List<string> photoExtensions)
{
int coreCount = Environment.ProcessorCount;
//limit the max paralel tasks and split the total amount of files by the limit set
int coreCount = Environment.ProcessorCount/2;
int totalFiles = allFiles.Count;
int chunkSize = (int)Math.Ceiling((double)totalFiles / coreCount);
//concurrent bag instead of lists due to multi tasks
var musicBag = new ConcurrentBag<string>();
var videoBag = new ConcurrentBag<string>();
var photoBag = new ConcurrentBag<string>();
//a list of tasks. needed to check if all are done
var tasks = new List<Task>();
for (int i = 0; i < coreCount; i++)
@@ -41,11 +46,15 @@ public class FileClassifier
musicBag.Add(file);
else if (videoExtensions.Contains(ext))
videoBag.Add(file);
else if (photoExtensions.Contains(ext))
{
photoBag.Add(file);
}
}
}));
}
//wait till all tasks are done and return 2 lists
await Task.WhenAll(tasks);
return (new List<string>(musicBag), new List<string>(videoBag));
return (new List<string>(musicBag), new List<string>(videoBag), new List<string>(photoBag));
}
}

View File

@@ -7,6 +7,7 @@ using System.Threading.Tasks;
public class FileScanner
{
//all extensions and cuncurrent bag as a list wich works threw out multiple threads
private readonly string[] _extensions;
private readonly ConcurrentBag<string> _foundFiles = new ConcurrentBag<string>();
@@ -14,7 +15,7 @@ public class FileScanner
{
_extensions = extensions.Select(e => e.ToLower()).ToArray();
}
//return a list of all coneckted drives
public async Task<List<string>> ScanAllDrivesAsync()
{
var drives = new List<string>();
@@ -31,14 +32,15 @@ public class FileScanner
return drives.ToList();
}
//scan teh designated drive or folder
public async Task<List<string>> ScanDriveParallel(string rootPath)
{
//all sub direcktorys will be also added to this bag
var folderQueue = new ConcurrentQueue<string>();
folderQueue.Enqueue(rootPath);
//all aktive skanners are placed here
var folderWorkers = new List<Task>();
//add Preformance testing here later
//keep max tasks at half teh amounts of cores
int maxWorkers = Environment.ProcessorCount/2;
for (int i = 0; i < maxWorkers; i++)
@@ -72,8 +74,9 @@ public class FileScanner
}
}));
}
//wait till all tasks are done
Task.WaitAll(folderWorkers.ToArray());
//return the found paths as string
return _foundFiles.ToList();
}
}