Advertisement
moriarty41

Task2

Jan 24th, 2021
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace ConsoleApp2
  7. {
  8.     public class CreateFileOrFolder
  9.     {
  10.         public static string GetRandomAlphaNumeric()
  11.         {
  12.             return Path.GetRandomFileName().Replace(".", "").Substring(0, 10);
  13.         }
  14.  
  15.         static List<string> EveryNthElement(List<string> list, int nth)
  16.         {
  17.             IEnumerable<string> result = Enumerable.Empty<string>();
  18.             result = list.Where((x, i) => i % nth != nth - 1);
  19.             return result.ToList();
  20.         }
  21.         static void Main()
  22.         {
  23.             string folderName = @"c:\Task2\Proba4";
  24.  
  25.             string pathString = System.IO.Path.Combine(folderName, "Files");
  26.             var directory = System.IO.Directory.CreateDirectory(pathString);
  27.  
  28.             string pathString1 = System.IO.Path.Combine(folderName, "files_generated.txt");
  29.             var filesGenerated = System.IO.File.Create(pathString1);
  30.  
  31.             List<string> list = new List<string>();
  32.  
  33.             using (System.IO.StreamWriter file = new System.IO.StreamWriter(filesGenerated))
  34.             {
  35.                 for (int i = 0; i < 11; i++)
  36.                 {
  37.                     string fileName = GetRandomAlphaNumeric();
  38.                     list.Add(fileName + ".txt");
  39.                     file.WriteLine(fileName + ".txt");
  40.                     var pathString2 = System.IO.Path.Combine(pathString, fileName + ".txt");
  41.                     var newFile = System.IO.File.Create(pathString2);
  42.                     newFile.Close();
  43.                 }
  44.             }
  45.             filesGenerated.Close();
  46.  
  47.             Console.WriteLine("files_generated.txt: ");
  48.             Console.WriteLine(string.Join(",", list));
  49.             list = list.OrderBy(a => a).ToList();
  50.  
  51.             string pathString3 = System.IO.Path.Combine(folderName, "files_sorted.txt");
  52.             var filesSorted = System.IO.File.Create(pathString3);
  53.  
  54.             using (System.IO.StreamWriter file = new System.IO.StreamWriter(filesSorted))
  55.             {
  56.                 for (int i = 0; i < list.Count; i++)
  57.                 {
  58.                     file.WriteLine(list[i]);
  59.                 }
  60.             }
  61.             filesSorted.Close();
  62.  
  63.             Console.WriteLine("files_sorted.txt: ");
  64.             Console.WriteLine(string.Join(",", list));
  65.  
  66.             string pathString4 = System.IO.Path.Combine(folderName, "files_deleted_1.txt");
  67.             string pathString5 = System.IO.Path.Combine(folderName, "files_deleted_2.txt");
  68.  
  69.             var filesDeleted1List = EveryNthElement(list, 2);
  70.             var filesDeleted2List = EveryNthElement(filesDeleted1List, 3);
  71.  
  72.             Console.WriteLine("files_deleted1.txt: ");
  73.             Console.WriteLine(string.Join(",", filesDeleted1List));
  74.             Console.WriteLine("files_deleted2.txt: ");
  75.             Console.WriteLine(string.Join(",", filesDeleted2List));
  76.  
  77.             var filesDeleted1 = System.IO.File.Create(pathString4);
  78.             using (System.IO.StreamWriter file = new System.IO.StreamWriter(filesDeleted1))
  79.             {
  80.                 for (int i = 0; i < filesDeleted1List.Count; i++)
  81.                 {
  82.                     file.WriteLine(filesDeleted1List[i]);
  83.                 }
  84.             }
  85.             filesDeleted1.Close();
  86.  
  87.             var filesDeleted2 = System.IO.File.Create(pathString5);
  88.  
  89.             using (System.IO.StreamWriter file2 = new System.IO.StreamWriter(filesDeleted2))
  90.             {
  91.                 for (int i = 0; i < filesDeleted2List.Count; i++)
  92.                 {
  93.                     file2.WriteLine(filesDeleted2List[i]);
  94.                 }
  95.             }
  96.             filesDeleted2.Close();
  97.  
  98.             Console.WriteLine("Files (files left total):  ");
  99.             Console.WriteLine(string.Join(",", list));
  100.  
  101.             string pathStringDelete = System.IO.Path.Combine(folderName, "Files");
  102.  
  103.             list = filesDeleted2List;
  104.  
  105.             Console.WriteLine("FilesX (after delete files from Files that are not listed in the file_deleted_2.txt - so only files that are in file_deleted_2: ");
  106.             Console.WriteLine(string.Join(",", list));
  107.  
  108.             System.IO.DirectoryInfo di = new DirectoryInfo(pathStringDelete);
  109.  
  110.             foreach (FileInfo file in di.GetFiles())
  111.             {
  112.                 bool da = false;
  113.                 for (int i=0; i<list.Count; i++)
  114.                 {
  115.                     if (file.Name == list[i].ToString()) da = true;
  116.                 }
  117.                 if (!da) file.Delete();
  118.             }
  119.  
  120.             string pathStringX = System.IO.Path.Combine(folderName, "files_" + list.Count + ".txt");
  121.             var filesX = System.IO.File.Create(pathStringX);
  122.  
  123.             using (System.IO.StreamWriter file = new System.IO.StreamWriter(filesX))
  124.             {
  125.                 for (int i = 0; i < list.Count; i++)
  126.                 {
  127.                     if (File.Exists(Path.Combine(pathStringDelete, list[i])))
  128.                     {
  129.                         file.WriteLine(list[i]);
  130.                     }
  131.                 }
  132.             }
  133.  
  134.             System.Console.WriteLine("Press any key to exit.");
  135.             System.Console.ReadKey();
  136.         }
  137.     }
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement