spicemustflow

smf_sortcleaner

Oct 21st, 2017
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.34 KB | None | 0 0
  1. /*
  2. @echo off && cls
  3. set WinDirNet=%WinDir%\Microsoft.NET\Framework
  4. IF EXIST "%WinDirNet%\v2.0.50727\csc.exe" set csc="%WinDirNet%\v2.0.50727\csc.exe"
  5. IF EXIST "%WinDirNet%\v3.5\csc.exe" set csc="%WinDirNet%\v3.5\csc.exe"
  6. IF EXIST "%WinDirNet%\v4.0.30319\csc.exe" set csc="%WinDirNet%\v4.0.30319\csc.exe"
  7. %csc% /nologo /out:"%~0.exe" %0
  8. "%~0.exe" %1
  9. del "%~0.exe"
  10. exit
  11. */
  12.  
  13. //14 mar 2015 @ 13:29
  14. //metaspamer.blogspot.com
  15.  
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Text;
  20.  
  21. namespace spicemustflow_sortcleaner
  22. {
  23.     class Program
  24.     {
  25.         static string GetFilename(string nameWithoutExtension, bool uniq)
  26.         {
  27.             int n = 1;
  28.             string outputFile = string.Empty;
  29.             while (true)
  30.             {
  31.                 outputFile = string.Format("{0}_{1}{2}.txt",
  32.                     nameWithoutExtension, uniq ? "uniques" : "duplicates",
  33.                     n > 1 ? n.ToString() : !!)
  34.                 if (File.Exists(outputFile)) n++;
  35.                 else break;
  36.             }
  37.             return outputFile;
  38.         }
  39.  
  40.         static void Main(string[] args)
  41.         {
  42.             string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\";
  43.             string[] filelist = null;
  44.             if (args.Length == 0) filelist = Directory.GetFiles(path, "*.txt", SearchOption.TopDirectoryOnly);
  45.             else filelist = args;
  46.  
  47.             foreach (string filename in filelist)
  48.             {
  49.                 Encoding enc = Encoding.Default;
  50.                 using (FileStream fs = File.OpenRead(filename))
  51.                 {
  52.                     byte[] data = new byte[3];
  53.                     while (fs.Read(data, 0, data.Length) > 0)
  54.                         if (data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf)
  55.                         {
  56.                             enc = Encoding.UTF8;
  57.                             break;
  58.                         }
  59.                         else
  60.                         {
  61.                             enc = Encoding.GetEncoding(1251);
  62.                             break;
  63.                         }
  64.                 }
  65.  
  66.                 Console.Write("loading {0}..\n", filename);
  67.                 string[] input = File.ReadAllLines(filename, enc);
  68.                 if (input.Length > 0)
  69.                 {
  70.                     Console.Write("sorting..\n");
  71.                     Array.Sort(input);
  72.                     List<string> uniques = new List<string>();
  73.                     List<string> duplicates = new List<string>();
  74.                     Console.Write("deleting duplicates..\n");
  75.                     uniques.Add(input[0]);
  76.                     for (int i = 1; i < input.Length; i++)
  77.                         if (input[i] != input[i - 1])
  78.                             uniques.Add(input[i]);
  79.                         else
  80.                             duplicates.Add(input[i]);
  81.  
  82.                     Console.Write("\nsaving..\n");
  83.                     File.WriteAllLines(path + GetFilename(Path.GetFileNameWithoutExtension(filename), true), uniques.ToArray(), enc);
  84.                     File.WriteAllLines(path + GetFilename(Path.GetFileNameWithoutExtension(filename), false), duplicates.ToArray(), enc);
  85.                     Console.Write("\n\n");
  86.                 }
  87.             }
  88.         }
  89.     }
  90. }
Add Comment
Please, Sign In to add comment