Guest User

EQMFer

a guest
Nov 30th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace EQMFer
  9. {
  10.     class EQMF
  11.     {
  12.         string currentDirectoryPath = "";
  13.         List<string> allFriends;
  14.         IEnumerable<string> files;
  15.         static void Main(string[] args)
  16.         {
  17.             EQMF eqMF = new EQMF();
  18.             eqMF.allFriends = new List<string>();
  19.             eqMF.FindCurrentDirectory();
  20.             eqMF.FindMatchingFiles();
  21.             eqMF.BuildFriendsList();
  22.             eqMF.RemoveDuplicates();
  23.             eqMF.CreateNewFiles();
  24.  
  25.            
  26.         }
  27.  
  28.         public string FindCurrentDirectory()
  29.         {
  30.             currentDirectoryPath = Directory.GetCurrentDirectory();
  31.         //    Console.WriteLine("Path is: " + currentDirectoryPath);
  32.            
  33.             return currentDirectoryPath;
  34.         }
  35.  
  36.         public void FindMatchingFiles()
  37.         {
  38.            files  = from f in Directory.EnumerateFiles(currentDirectoryPath)
  39.                         where f.EndsWith("_project1999.ini") && ! (f.Contains("UI_"))
  40.                         select f;
  41.           //  foreach (string aFile in files)
  42.           //      Console.WriteLine(aFile);
  43.             Console.WriteLine("Total Character Files Found: " + files.Count());
  44.         }
  45.  
  46.         public void BuildFriendsList()
  47.         {
  48.         //    Console.WriteLine("FilesFound:");
  49.             foreach (string aFile in files)
  50.             {
  51.                // Console.WriteLine(aFile);
  52.                 ExtractFriendsFromFile(aFile);
  53.             }
  54.       //      Console.ReadLine();
  55.         }
  56.  
  57.         public void ExtractFriendsFromFile(string fileName)
  58.         {
  59.             using (StreamReader sr = File.OpenText(fileName))
  60.             {
  61.                 string s = String.Empty;
  62.                 while ((s = sr.ReadLine()) != null)
  63.                 {
  64.                     if(s.Contains("Friend") && !s.Contains("NULL") && !s.Contains("["))
  65.                     {
  66.                         string[] friend = s.Split('=');
  67.                        
  68.                         allFriends.Add(friend[1]);
  69.  
  70.                     }
  71.                 }
  72.             }
  73.  
  74.            
  75.            
  76.         }
  77.  
  78.         public void RemoveDuplicates()
  79.         {
  80.             List<string> distinct = allFriends.Distinct().ToList();
  81.             allFriends = distinct;
  82.             Console.WriteLine("Total amount of friends is: " + allFriends.Count);
  83.          //   Console.ReadLine();
  84.            
  85.         }
  86.  
  87.         public void CreateNewFiles()
  88.         {
  89.            
  90.             int friendCount = allFriends.Count;
  91.             int processingFriend = 0;
  92.             int fileCount = files.Count();
  93.  
  94.             for (int i = 0; i < fileCount; i++)
  95.             {
  96.                 using (var input = File.OpenText(files.ElementAt(i)))
  97.                 using (var output = new StreamWriter("temp.ini"))
  98.                 {
  99.                     string line;
  100.                     while (null != (line = input.ReadLine()))
  101.                     {
  102.                         if (line.StartsWith("Friend"))
  103.                         {
  104.                             if (processingFriend < friendCount)
  105.                                 line = "Friend" + processingFriend + "=" + allFriends.ElementAt(processingFriend);
  106.                             else
  107.                                 line = "Friend" + processingFriend + "=*NULL*";
  108.                             processingFriend++;
  109.                         }
  110.                         output.WriteLine(line);
  111.                       //  Console.WriteLine("Wrote: " + line);
  112.  
  113.                     }
  114.  
  115.                 }
  116.                 string filetoreplace = files.ElementAt(i);
  117.             //    Console.WriteLine("Replacing: " + files.ElementAt(i));
  118.                 File.Delete(files.ElementAt(i));
  119.            //     Console.WriteLine(filetoreplace + " Exists? " + File.Exists(filetoreplace));
  120.                
  121.            //     Console.WriteLine("Moving temp.ini to " + filetoreplace);
  122.                 File.Move("temp.ini", filetoreplace);
  123.  
  124.                
  125.                 processingFriend = 0;
  126.             }
  127.             Console.WriteLine("Finished merge!  Press any key to exit");
  128.             Console.ReadLine();
  129.         }
  130.  
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment