-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirCache.cs
187 lines (167 loc) · 4.97 KB
/
DirCache.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
//
// Main website for TVRename is http://tvrename.com
//
// Source code available at https://github.com/TV-Rename/tvrename
//
// Copyright (c) TV Rename. This code is released under GPLv3 https://github.com/TV-Rename/tvrename/blob/master/LICENSE.md
//
using System;
using System.Collections.Generic;
using System.Linq;
using Alphaleonis.Win32.Filesystem;
// Recursively reads and caches files and folders, and info about them, as this is way faster
// than repeatedly hitting the filesystem.
namespace TVRename
{
public class DirCache : List<DirCacheEntry>
{
public DirCache()
{
}
public DirCache(SetProgressDelegate prog, string folder, bool subFolders)
{
BuildDirCache(prog, 0, 0, folder, subFolders);
}
public static int CountFiles(string folder, bool subFolders)
{
int n = 0;
if (!Directory.Exists(folder))
{
return n;
}
DirectoryInfo di = new DirectoryInfo(folder);
try
{
n = di.GetFiles().Length;
}
catch (NotSupportedException)
{
}
catch (UnauthorizedAccessException)
{
}
catch (System.IO.DirectoryNotFoundException)
{
}
catch (System.IO.IOException)
{
}
if (!subFolders)
{
return n;
}
DirectoryInfo[] dirs =new DirectoryInfo[0];
try
{
dirs = di.GetDirectories();
}
catch (NotSupportedException)
{
}
catch (UnauthorizedAccessException)
{
}
catch (System.IO.DirectoryNotFoundException)
{
}
catch (System.IO.IOException)
{
}
n += dirs.Sum(di2 => CountFiles(di2.FullName, true));
return n;
}
public void AddFolder(SetProgressDelegate prog, int initialCount, int totalFiles, string folder,
bool subFolders)
{
BuildDirCache(prog, initialCount, totalFiles, folder, subFolders);
}
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
private int BuildDirCache(SetProgressDelegate prog, int count, int totalFiles, string folder, bool subFolders)
{
if (!Directory.Exists(folder))
{
Logger.Warn("The search folder \"" + folder + " does not exist.");
return count;
}
try
{
DirectoryInfo di = new DirectoryInfo(folder);
if (!di.Exists)
{
return count;
}
foreach (FileInfo ff in GetFiles(di))
{
count++;
Add(new DirCacheEntry(ff));
if ((prog != null) && (totalFiles != 0))
{
prog.Invoke(100 * (count) / totalFiles, string.Empty);
}
}
if (subFolders)
{
foreach (DirectoryInfo di2 in GetDirectoryInfo(di))
{
count += BuildDirCache(prog, count, totalFiles, di2.FullName, true);
}
}
}
catch (Exception exception)
{
Logger.Error(exception);
}
return count;
}
private static IEnumerable<DirectoryInfo> GetDirectoryInfo(DirectoryInfo di)
{
DirectoryInfo[] dirs = new DirectoryInfo[0];
try
{
dirs = di.GetDirectories();
}
catch (NotSupportedException e)
{
Logger.Info(e);
}
catch (UnauthorizedAccessException e)
{
Logger.Info(e);
}
catch (System.IO.DirectoryNotFoundException e)
{
Logger.Info(e);
}
catch (System.IO.IOException e)
{
Logger.Info(e);
}
return dirs;
}
private static IEnumerable<FileInfo> GetFiles(DirectoryInfo di)
{
FileInfo[] f2 = new FileInfo[0];
try
{
f2 = di.GetFiles();
}
catch (NotSupportedException e)
{
Logger.Info(e);
}
catch (UnauthorizedAccessException e)
{
Logger.Info(e);
}
catch (System.IO.DirectoryNotFoundException e)
{
Logger.Info(e);
}
catch (System.IO.IOException e)
{
Logger.Info(e);
}
return f2;
}
}
}