-
Notifications
You must be signed in to change notification settings - Fork 0
/
FolderAnalyzer.cs
243 lines (207 loc) · 7.92 KB
/
FolderAnalyzer.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DuplicateDetector
{
public enum EventType
{
Message = 0,
Warring = 1,
Error = 2
}
class FolderAnalyzer
{
public delegate void ScanStartedHandler();
public delegate void ScanCanceledHandler();
public delegate void ScanFinishedHandler(bool succes);
public delegate void NotificationEventHandler(string message, EventType type);
public event ScanStartedHandler ScanStarted;
public event ScanCanceledHandler ScanCanceled;
public event ScanFinishedHandler ScanFinished;
public event NotificationEventHandler Notification;
private List<MyFile> _files;
private List<MyFile> _processedFiles = new List<MyFile>();
public FolderAnalyzer()
{
_files = new List<MyFile>();
this.IgnoreFolders = new StringCollection();
}
public bool IsRunning { get; set; }
public string FolderToScan { get; set; }
public StringCollection PriorityFolders { get; set; }
public StringCollection IgnoreFolders{ get; set; }
public bool CancelInProgress { get; set; }
public List<MyFile> GetResult()
{
return _processedFiles;
}
private void OnScanStarted()
{
this.IsRunning = true;
if (ScanStarted != null)
{
ScanStarted();
}
}
private void OnScanCanceled()
{
this.IsRunning = false;
this.CancelInProgress = false;
if (ScanCanceled != null)
{
ScanCanceled();
}
}
private void OnScanFinished(bool succes)
{
this.IsRunning = false;
if (ScanFinished != null)
{
ScanFinished(succes);
}
}
private void OnNotification(string message, EventType type = EventType.Message)
{
//error and no listeners
if (Notification != null)
{
Notification(message, type);
}
else
{
if (type == EventType.Error)
{
throw new Exception(message);
}
}
}
public bool StartScan(string folder, StringCollection priorityFolders, StringCollection ignoreFolders)
{
DateTime startTime = DateTime.Now;
bool retValue = false;
this.FolderToScan = folder;
this.PriorityFolders = priorityFolders;
this.IgnoreFolders = ignoreFolders;
_files.Clear();
_processedFiles.Clear();
if (ValidateFolders())
{
OnNotification("Validation succesfull");
retValue = true;
OnScanStarted();
ScanFolders(this.FolderToScan);
OnNotification(string.Format("Total files found {0}", _files.Count));
AnalyzeFiles();
retValue = true;
}
else
{
OnNotification("Validation failed", EventType.Error);
}
TimeSpan duration = DateTime.Now - startTime;
OnNotification(string.Format("Scan duration {0} hours {1} minutes {2} seconds", duration.Hours, duration.Minutes, duration.Seconds));
if (this.CancelInProgress)
{
OnScanCanceled();
}
else
{
OnScanFinished(retValue);
}
return retValue;
}
private bool ValidateFolders()
{
bool retValue = true;
if (!Directory.Exists(this.FolderToScan))
{
retValue = false;
this.OnNotification(string.Format("Folder {0} doesn't exists!", this.FolderToScan), EventType.Error);
}
foreach (string folder in this.PriorityFolders)
{
if (!Directory.Exists(folder))
{
retValue = false;
this.OnNotification(string.Format("Folder {0} doesn't exists!", folder), EventType.Error);
}
}
return retValue;
}
private void AnalyzeFiles()
{
//TODO: implement async...for md5hash
OnNotification(string.Format("File compare started! {0} files to process", _files.Count));
int duplicatesCount = 0;
while (_files.Count() > 0)
{
List<MyFile> fileGroup = new List<MyFile>();
MyFile file = _files[0];
fileGroup.Add(file);
_files.Remove(file);
MyFile originalFile;
var duplicates = _files.Where(item => item.FileName == file.FileName && item.Size == file.Size && item.DateModified == file.DateModified).ToList<MyFile>();
if (duplicates.Count > 0)
{
_files.RemoveAll(item => item.FileName == file.FileName && item.Size == file.Size && item.DateModified == file.DateModified);
fileGroup.AddRange(duplicates);
originalFile = fileGroup.Where(item => !item.IsInLowPriorityFolder).FirstOrDefault();
if (originalFile == null)
{
originalFile = fileGroup.OrderBy(item => item.DateModified).FirstOrDefault();
}
fileGroup.Remove(originalFile);
if (fileGroup.Count > 0)
{
originalFile.SameFiles = fileGroup;
duplicatesCount = duplicatesCount + fileGroup.Count();
}
_processedFiles.Add(originalFile);
if (this.CancelInProgress) break;
}
}
OnNotification(string.Format("Found {0} files with duplicates and total {1} duplicates", _processedFiles.Count(), duplicatesCount));
}
public void CancelScan()
{
this.CancelInProgress = true;
OnNotification("Canceling scan");
}
public void ScanFolders(string folderToScan)
{
try
{
DirectoryInfo dirInfo =null;
StringCollection foldersToIgnore = new StringCollection();
foreach (var item in IgnoreFolders)
{
foldersToIgnore.Add(item);
dirInfo = new DirectoryInfo(item);
var items = dirInfo.EnumerateDirectories("*.*",SearchOption.AllDirectories);
foldersToIgnore.AddRange(items.Select(f => f.FullName).ToArray<string>());
}
dirInfo = new DirectoryInfo(folderToScan);
var result = from file in dirInfo.EnumerateFiles("*.*", SearchOption.AllDirectories)
where (!file.Attributes.HasFlag(FileAttributes.System) && !foldersToIgnore.Contains(file.DirectoryName))
select new MyFile
(
file.Name,
file.DirectoryName,
file.Length,
file.LastWriteTime,
PriorityFolders.Contains(file.DirectoryName)
);
_files = result.ToList<MyFile>();
OnNotification(string.Format("{0} files found.", _files.Count()));
}
catch (Exception ex)
{
OnNotification(ex.ToString(), EventType.Error);
}
}
}
}