Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection.Emit;
- using System.Text;
- using static System.Net.WebRequestMethods;
- namespace DSA
- {
- public class FileUtils
- {
- public const string Path = @"..\..\..\Images";
- public static List<string> files = new List<string>();
- public static Dictionary<string, int> stats = new Dictionary<string, int>();
- public static string GetFileExtension(string filePath)
- {
- int lastIndex = filePath.LastIndexOf('.');
- return filePath[lastIndex..];
- }
- public static string GetFileName(string path)
- {
- int lastIndex = path.LastIndexOf('\\') + 1;
- string lastPart = path[lastIndex..];
- return lastPart;
- }
- public static string TraverseDirectories(string path, int level = 0)
- {
- var indentation = new string(' ', level * 2);
- var result = $"{indentation}{GetFileName(path)}:\n";
- foreach (var file in Directory.GetFiles(path))
- {
- result += $"{indentation + new string(' ', 2)}{GetFileName(file)}\n";
- }
- foreach (var directory in Directory.GetDirectories(path))
- {
- result += TraverseDirectories(directory, level + 1);
- }
- return result;
- }
- public static List<string> FindFiles(string path, string extension)
- {
- foreach (var file in Directory.GetFiles(path))
- {
- if (GetFileExtension(file).Equals(extension))
- {
- files.Add(GetFileName(file));
- }
- }
- foreach (var directory in Directory.GetDirectories(path))
- {
- FindFiles(directory, extension);
- }
- return files;
- }
- public static bool FileExists(string path, string fileName)
- {
- foreach (var file in Directory.GetFiles(path))
- {
- if (GetFileName(file).Equals(fileName))
- {
- return true;
- }
- }
- foreach (var directory in Directory.GetDirectories(path))
- {
- if (FileExists(directory, fileName))
- {
- return true;
- }
- }
- return false;
- }
- public static Dictionary<string, int> GetDirectoryStats(string path)
- {
- foreach (var file in Directory.GetFiles(path))
- {
- var extension = GetFileExtension(file);
- if (!stats.ContainsKey(extension))
- {
- stats[extension] = 0;
- }
- stats[extension]++;
- }
- foreach (var directory in Directory.GetDirectories(path))
- {
- GetDirectoryStats(directory);
- }
- return stats;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement