Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace EQMFer
- {
- class EQMF
- {
- string currentDirectoryPath = "";
- List<string> allFriends;
- IEnumerable<string> files;
- static void Main(string[] args)
- {
- EQMF eqMF = new EQMF();
- eqMF.allFriends = new List<string>();
- eqMF.FindCurrentDirectory();
- eqMF.FindMatchingFiles();
- eqMF.BuildFriendsList();
- eqMF.RemoveDuplicates();
- eqMF.CreateNewFiles();
- }
- public string FindCurrentDirectory()
- {
- currentDirectoryPath = Directory.GetCurrentDirectory();
- // Console.WriteLine("Path is: " + currentDirectoryPath);
- return currentDirectoryPath;
- }
- public void FindMatchingFiles()
- {
- files = from f in Directory.EnumerateFiles(currentDirectoryPath)
- where f.EndsWith("_project1999.ini") && ! (f.Contains("UI_"))
- select f;
- // foreach (string aFile in files)
- // Console.WriteLine(aFile);
- Console.WriteLine("Total Character Files Found: " + files.Count());
- }
- public void BuildFriendsList()
- {
- // Console.WriteLine("FilesFound:");
- foreach (string aFile in files)
- {
- // Console.WriteLine(aFile);
- ExtractFriendsFromFile(aFile);
- }
- // Console.ReadLine();
- }
- public void ExtractFriendsFromFile(string fileName)
- {
- using (StreamReader sr = File.OpenText(fileName))
- {
- string s = String.Empty;
- while ((s = sr.ReadLine()) != null)
- {
- if(s.Contains("Friend") && !s.Contains("NULL") && !s.Contains("["))
- {
- string[] friend = s.Split('=');
- allFriends.Add(friend[1]);
- }
- }
- }
- }
- public void RemoveDuplicates()
- {
- List<string> distinct = allFriends.Distinct().ToList();
- allFriends = distinct;
- Console.WriteLine("Total amount of friends is: " + allFriends.Count);
- // Console.ReadLine();
- }
- public void CreateNewFiles()
- {
- int friendCount = allFriends.Count;
- int processingFriend = 0;
- int fileCount = files.Count();
- for (int i = 0; i < fileCount; i++)
- {
- using (var input = File.OpenText(files.ElementAt(i)))
- using (var output = new StreamWriter("temp.ini"))
- {
- string line;
- while (null != (line = input.ReadLine()))
- {
- if (line.StartsWith("Friend"))
- {
- if (processingFriend < friendCount)
- line = "Friend" + processingFriend + "=" + allFriends.ElementAt(processingFriend);
- else
- line = "Friend" + processingFriend + "=*NULL*";
- processingFriend++;
- }
- output.WriteLine(line);
- // Console.WriteLine("Wrote: " + line);
- }
- }
- string filetoreplace = files.ElementAt(i);
- // Console.WriteLine("Replacing: " + files.ElementAt(i));
- File.Delete(files.ElementAt(i));
- // Console.WriteLine(filetoreplace + " Exists? " + File.Exists(filetoreplace));
- // Console.WriteLine("Moving temp.ini to " + filetoreplace);
- File.Move("temp.ini", filetoreplace);
- processingFriend = 0;
- }
- Console.WriteLine("Finished merge! Press any key to exit");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment