-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program.cs
112 lines (89 loc) · 2.57 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace bomremover
{
class Program
{
private static string _searchPattern;
private static List<string> _checkList = new List<string>();
static void Main(string[] args)
{
if (args.Length != 2)
{
String filename = Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location);
Console.WriteLine("Usage: {0} [basedirectory] [filesearchpattern]", filename);
Console.WriteLine("i.e.: {0} C:\\work\\project *.java", filename);
return;
}
var baseDirectory = args[0];
var searchPattern = args[1];
_searchPattern = searchPattern;
AddFilesFromFolderToDeleteListRecursive(baseDirectory);
if (_checkList.Count == 0)
{
Console.WriteLine("No matching file found. Press Enter to Exit...");
Console.ReadLine();
return;
}
Console.WriteLine("Press enter to process all files in the list. ({0} files in total)", _checkList.Count);
Console.ReadLine();
DeleteFilesFromDeleteList();
Console.WriteLine("Finished. Press Enter to Exit");
Console.ReadLine();
}
private static void DeleteFilesFromDeleteList()
{
foreach (var f in _checkList)
{
try
{
Console.WriteLine("Processing file '{0}'...", f);
byte[] content = File.ReadAllBytes(f);
byte[] newArray = new byte[content.Length - 3];
Array.Copy(content, 3, newArray, 0, newArray.Length);
File.WriteAllBytes(f, newArray);
}
catch (Exception ex)
{
Console.WriteLine("Could not replace file '{0}': {1}", f, ex.ToString());
}
}
}
private static void AddFilesFromFolderToDeleteListRecursive(string basedir)
{
foreach (var d in Directory.GetDirectories(basedir))
{
AddFilesFromFolderToDeleteListRecursive(d);
}
foreach (var f in Directory.GetFiles(basedir, _searchPattern))
{
try
{
var filestream = File.OpenRead(f);
if (filestream.Length >= 3)
{
var buffer = new byte[3];
filestream.Read(buffer, 0, buffer.Length);
if (buffer[0] == 0xEF && buffer[1] == 0xBB && buffer[2] == 0xBF)
{
AddToDeleteList(f);
}
filestream.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Could not check file '{0}': {1}", f, ex.ToString());
}
}
}
private static void AddToDeleteList(string f)
{
Console.WriteLine(" Added '{0}' to processing list.", f);
_checkList.Add(f);
}
}
}