Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.IO;
  3.  
  4. namespace SeparateFiles
  5. {
  6.     public class FileSplit
  7.     {
  8.         private string mainPath;
  9.         private string partsPath;
  10.         private string howManyFiles;
  11.         private int numberOfFiles = 2;
  12.  
  13.         public FileSplit(string mainFilePath)
  14.         {
  15.             MainFilePath = mainFilePath;
  16.         }
  17.  
  18.         public FileSplit(string partsDirectory, string howManyFiles)
  19.         {
  20.             PartsDirectory = partsDirectory;
  21.             HowManyFiles = howManyFiles;
  22.         }
  23.  
  24.         public string MainFilePath
  25.         {
  26.             get => mainPath;
  27.             set
  28.             {
  29.                 mainPath = UserMessages.PromptUserForAnswer(UserMessages.AskWhichFile);
  30.             }
  31.         }
  32.  
  33.         public string PartsDirectory
  34.         {
  35.             get => partsPath;
  36.             set
  37.             {
  38.                 partsPath = UserMessages.PromptUserForAnswer(UserMessages.AskWhereToSave);
  39.             }
  40.         }
  41.  
  42.         public string HowManyFiles
  43.         {
  44.             get => howManyFiles;
  45.             set
  46.             {
  47.                 howManyFiles = UserMessages.PromptUserForAnswer(UserMessages.AskHowManyFiles);
  48.             }
  49.         }
  50.  
  51.         private void Read(string mainPath, string howManyFiles, string partsPath)
  52.         {
  53.             var lines = new List<string>();
  54.  
  55.             using (StreamReader sr = new StreamReader(mainPath))
  56.             {
  57.                 string line;
  58.  
  59.                 while ((line = sr.ReadLine()) != null)
  60.                 {
  61.                     lines.Add(line);
  62.                 }
  63.             }
  64.  
  65.             if (lines.Count > 2)
  66.             {
  67.                 numberOfFiles = int.TryParse(howManyFiles, out numberOfFiles) ? numberOfFiles : 2;
  68.             }
  69.  
  70.             Write(lines, partsPath);
  71.         }
  72.  
  73.         private void Write(List<string> lines, string partsPath)
  74.         {            
  75.             var fileCounter = 1;
  76.             var linesPerFile = lines.Count / numberOfFiles;
  77.             var currEndLine = linesPerFile;
  78.  
  79.             var stream = new StreamWriter($"{partsPath}/{fileCounter}. file.csv");
  80.  
  81.             for (int row = 0; row < lines.Count; row++)
  82.             {
  83.                 stream.WriteLine(lines[row]);
  84.  
  85.                 if (row == currEndLine - 1 && fileCounter < numberOfFiles)
  86.                 {
  87.                     fileCounter++;
  88.  
  89.                     if (fileCounter == numberOfFiles)
  90.                     {
  91.                         currEndLine = lines.Count;
  92.                     }
  93.                     else
  94.                     {
  95.                         currEndLine += linesPerFile;
  96.                     }
  97.                     stream.Close();
  98.                     stream = new StreamWriter($"{partsPath}/{fileCounter}. file.csv");
  99.                 }
  100.             }
  101.             stream.Close();
  102.         }
  103.  
  104.         public void Split(string mainPath, string howManyFiles, string partsPath)
  105.         {
  106.             Read(mainPath, howManyFiles, partsPath);
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement