-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
279 lines (247 loc) · 11.2 KB
/
Form1.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace FolderAnalytics
{
public partial class Form1 : Form
{
public bool IsProcess = false;
public uint DirCount = 0;
public uint FileCount = 0;
public uint ErrorCount = 0;
public Dictionary<string, uint> FilesExtension = new Dictionary<string, uint>();
public Dictionary<string, long> TotalFilesSizeWithExtension = new Dictionary<string, long>();
public Dictionary<string, string> TotalFilesPathWithExtension = new Dictionary<string, string>();
public Dictionary<string, Queue<string>> TotalFilesExtensionWithPath = new Dictionary<string, Queue<string>>();
public Form1()
{
InitializeComponent();
listView1.Columns.Add(new ColumnHeader() { Text = "Extension", Width = 200 });
listView1.Columns.Add(new ColumnHeader() { Text = "Quantity", Width = 150 });
listView1.Columns.Add(new ColumnHeader() { Text = "Overall size", Width = 100 });
listView1.View = View.Details;
}
public void MakeDataInfo()
{
listView1.Items.Clear();
Dictionary<string, uint> TempDictionary = new Dictionary<string, uint>();
foreach (var pair in FilesExtension.OrderByDescending(pair => pair.Value))
if (pair.Key != "")
{
//Console.WriteLine($"{pair.Key} x{pair.Value}");
TempDictionary.Add(pair.Key, pair.Value);
}
foreach (var pair in TotalFilesSizeWithExtension.OrderByDescending(pair => pair.Value))
if (pair.Key != "")
{
listView1.Items.Add(new ListViewItem(new string[] {
pair.Key,
TempDictionary[pair.Key].ToString(),
(Math.Round((float)(pair.Value) / 1024 / 1024, 2)).ToString() + "mb"
})
{ Tag = pair.Key });
}
WriteLine("Directories count - " + DirCount.ToString());
WriteLine("Files count - " + FileCount.ToString());
}
Stopwatch _sw = new Stopwatch();
uint _step = 0;
uint _error = 0;
public void ProcDir(string path)
{
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = { };
try { subdirectoryEntries = System.IO.Directory.GetDirectories(path); }
catch (Exception ex) { _error++; }
foreach (string subdirectory in subdirectoryEntries)
{
DirCount++;
ProcDir(subdirectory);
}
// Process the list of files found in the directory.
string[] fileEntries = { };
try { fileEntries = System.IO.Directory.GetFiles(path); }
catch (Exception ex) { _error++; }
foreach (string fileName in fileEntries)
{
_step++;
try
{
if (checkBox1.Checked && new FileInfo(fileName).Extension != textBox2.Text) continue;
FileCount++;
TotalFilesPathWithExtension.Add(fileName, new FileInfo(fileName).Extension);
if (FilesExtension.ContainsKey(new FileInfo(fileName).Extension))
FilesExtension[new FileInfo(fileName).Extension]++;
else
FilesExtension.Add(new FileInfo(fileName).Extension, 1);
if (TotalFilesSizeWithExtension.ContainsKey(new FileInfo(fileName).Extension))
TotalFilesSizeWithExtension[new FileInfo(fileName).Extension] += new FileInfo(fileName).Length;
else
TotalFilesSizeWithExtension.Add(new FileInfo(fileName).Extension, new FileInfo(fileName).Length);
if (TotalFilesExtensionWithPath.ContainsKey(new FileInfo(fileName).Extension))
TotalFilesExtensionWithPath[new FileInfo(fileName).Extension].Enqueue(fileName);
else
{
TotalFilesExtensionWithPath.Add(new FileInfo(fileName).Extension, new Queue<string>());
TotalFilesExtensionWithPath[new FileInfo(fileName).Extension].Enqueue(fileName);
}
}
catch (Exception ex)
{
_error++;
}
}
if (_sw.ElapsedMilliseconds >= 10)
{
var invok = BeginInvoke((Action)delegate
{
richTextBox1.Text =
$"\nLoad files" +
$"\nThe program may freeze" +
"\nItems processed : " + _step.ToString() +
"\nnErrors : " + _error.ToString() +
"\nError rate : " + (Math.Round((float)((float)(_error) / (float)(_step)), 8)*100).ToString() + "%";
label1.Text = path;
});
_sw.Restart();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (IsProcess)
{
MessageBox.Show("Wait for an already running process to finish !", "Problem",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
_step = 0;
_error = 0;
listView1.Items.Clear();
richTextBox1.Text = "Wait";
richTextBox1.Enabled = false;
IsProcess = true;
label1.Visible = true;
_sw.Start();
Task.Run(() => {
Stopwatch sw = new Stopwatch(); sw.Start();
ProcDir(textBox1.Text);
var invoke = BeginInvoke((Action)delegate
{
richTextBox1.Enabled = true;
richTextBox1.Clear();
richTextBox1.Text += $"\n\nProcess time - {sw.ElapsedMilliseconds}ms";
MakeDataInfo();
IsProcess = false;
label1.Visible = false;
});
});
}
private void WriteLine(string text) =>
richTextBox1.Text += $"\n{text}";
private void button3_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
listView1.Clear();
}
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (IsProcess)
{
MessageBox.Show("Wait for an already running process to finish !", "Problem",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
richTextBox1.Clear();
richTextBox1.Text = "Wait";
richTextBox1.Enabled = false;
IsProcess = true;
label1.Visible = true;
foreach (ListViewItem v in listView1.SelectedItems)
{
Task.Run(() =>
{
Stopwatch sw = new Stopwatch(); sw.Start();
string output = "";
uint step = 0;
/*foreach (string path in TotalFilesPathWithExtension.Keys)
{
if (new FileInfo(path).Extension == v.Tag.ToString())
{
step++;
output += "\n" + path;
if (sw.ElapsedMilliseconds >= 10)
{
var invok2 = BeginInvoke((Action)delegate
{
richTextBox1.Text =
$"\nПоиск {v.Tag.ToString()} файлов" +
"\nОбработанно элементов : " + step.ToString() +
"\nИз : " + FilesExtension[v.Text].ToString() +
"\nОбщий размер файлов с таким расширением : " + TotalFilesSizeWithExtension[v.Text].ToString();
label1.Text = path;
});
sw.Restart();
}
}
}*/
if (TotalFilesExtensionWithPath.ContainsKey(v.Text))
{
foreach (string path in TotalFilesExtensionWithPath[v.Text])
{
step++;
output += "\n" + path;
if (sw.ElapsedMilliseconds >= 10)
{
var invok2 = BeginInvoke((Action)delegate
{
richTextBox1.Text =
$"\nSearch {v.Tag.ToString()} files" +
"\nItems processed : " + step.ToString() +
"\nOf : " + FilesExtension[v.Text].ToString() +
"\nThe total size of files with this extension : " + TotalFilesSizeWithExtension[v.Text].ToString();
label1.Text = path;
});
sw.Restart();
}
}
}
var invoke = BeginInvoke((Action)delegate
{
richTextBox1.Enabled = true;
richTextBox1.Clear();
WriteLine(output);
IsProcess = false;
label1.Visible = false;
});
});
}
}
public static void GetAllFiles(string rootDirectory, List<string> files)
{
string[] directories = Directory.GetDirectories(rootDirectory);
files.AddRange(Directory.GetFiles(rootDirectory));
foreach (string path in directories)
GetAllFiles(path, files);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
this.textBox2.Enabled = checkBox1.Checked;
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult dialog = this.folderBrowserDialog1.ShowDialog();
if (dialog == DialogResult.OK)
{
this.textBox1.Text = this.folderBrowserDialog1.SelectedPath;
}
}
}
}