deleriumbg

Files

Jun 24th, 2018
2,620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Files
  6. {
  7.     class File
  8.     {
  9.         public string Root { get; set; }
  10.         public string FullName { get; set; }
  11.         public long Size { get; set; }
  12.  
  13.         public File(string root, string fullName, long size)
  14.         {
  15.             Root = root;
  16.             FullName = fullName;
  17.             Size = size;
  18.         }
  19.     }
  20.  
  21.     class Program
  22.     {
  23.         static void Main(string[] args)
  24.         {
  25.             int numberOfFiles = int.Parse(Console.ReadLine());
  26.             List<File> files = new List<File>();
  27.  
  28.             for (int i = 0; i < numberOfFiles; i++)
  29.             {
  30.                 string[] fullPath = Console.ReadLine().Split(new [] { '\\', ';' },StringSplitOptions.RemoveEmptyEntries);
  31.                 string rootDirectory = fullPath[0];
  32.                 string fullFileName = fullPath[fullPath.Length - 2];
  33.                 long fileSize = long.Parse(fullPath[fullPath.Length - 1]);
  34.  
  35.                 File file = new File(rootDirectory, fullFileName, fileSize);
  36.                 if (!files.Any(x => x.Root == rootDirectory && x.FullName == fullFileName))
  37.                 {
  38.                     files.Add(file);
  39.                 }
  40.                 else
  41.                 {
  42.                     File existingFile = files.First(x => x.Root == rootDirectory && x.FullName == fullFileName);
  43.                     existingFile.Size = fileSize;
  44.                 }
  45.             }
  46.  
  47.             string[] printCommand = Console.ReadLine().Split(new [] { " in " },StringSplitOptions.RemoveEmptyEntries);
  48.             string extension = printCommand[0];
  49.             string folder = printCommand[1];
  50.  
  51.             //Print all file names with a given extension that are present in a given root directory sorted by their file size in descending order. If two files have same size, order them by alphabetical order.
  52.             if (!files.Any(x => x.Root == folder && x.FullName.EndsWith(extension)))
  53.             {
  54.                 Console.WriteLine("No");
  55.                 return;
  56.             }
  57.  
  58.             foreach (var file in files.Where(x => x.Root == folder && x.FullName.EndsWith(extension))
  59.                 .OrderByDescending(x => x.Size).ThenBy(x => x.FullName))
  60.             {
  61.                 Console.WriteLine($"{file.FullName} - {file.Size} KB ");
  62.             }
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment