-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPickRandomFile.cs
More file actions
521 lines (454 loc) · 18.1 KB
/
Copy pathPickRandomFile.cs
File metadata and controls
521 lines (454 loc) · 18.1 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
using System;
using System.Diagnostics;
using System.Collections.Generic;
//using System.Linq;
using System.Xml.Serialization;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace RandomFilePicker
{
public static class PickRandomFile
{
private static double tim_dir = 0;
private static double tim_file = 0;
private static int dirs_scanned = 0;
private static int files_scanned = 0;
public static List<DirectoryInfo> folderList = new List<DirectoryInfo>();
public static List<DirectoryInfo> excludeList = new List<DirectoryInfo>();
public static List<string> fileTypeList = new List<string>();
public static List<extAppPair> appOverrides = new List<extAppPair>();
public static List<FileShortInfo> allFiles = new List<FileShortInfo>();
//if shuffle mode enabled, files that have been picked get moved here, then the lists are swapped round once all
//have been picked. Prevents same files from being picked multiple times.
public static List<FileShortInfo> pickedFiles = new List<FileShortInfo>();
public static bool recurse = true;
public static bool usingFileTypes = false;
public static bool firstfileonly = false;
public static bool hideSpawnedWindows = false;
public static bool shuffle = false;
public static bool showStats = false;
public static bool useCache = false;
public static bool showCacheStats = false;
public static bool ignoreMissingFolders = false;
public static int cacheHoursMin = 0;
public static int cacheHoursMax = 0;
public static int threadWaitDuration = 0;
public static int threadRepeatCount = -1;
public static bool showChooser = false;
public static string fileFilter = "";
public static string cachefolder = "";
private static FileCache cache;
public static void scanDirectories()
{
try
{
tim_file = 0;
tim_dir = 0;
dirs_scanned = 0;
files_scanned = 0;
DateTime time_start = DateTime.Now;
//if in cache mode, load the cache.
try
{
if (useCache) { cache = new FileCache(); }
}
catch (Exception e)
{
throw new Exception("Error loading cache", e);
}
//add all files from our pathlist to a list
List<string> folders = new List<string>();
try
{
foreach (DirectoryInfo d in PickRandomFile.folderList)
{
if (d.Exists)
{
try
{
folders.Add(d.FullName);
}
catch
{
MessageBox.Show("Directory didnt have a fullname?");
MessageBox.Show(d.Name);
}
//add all subfolders
if (recurse)
{
foreach (DirectoryInfo di in d.GetDirectories("*", SearchOption.AllDirectories))
{
if (di.Exists) { folders.Add(di.FullName); }
}
}
}
else if (!ignoreMissingFolders)
{
MessageBox.Show("Directory " + d.FullName + " doesnt exist");
}
}
tim_dir = DateTime.Now.Subtract(time_start).Seconds;
}
catch (Exception e)
{
throw new Exception("Error during directory sweep", e);
}
//Now check for files (or use cache where appropriate)
try
{
foreach (String s in folders)
{
List<FileShortInfo> files = null;
if (useCache)
{
files = cache.getFilesFromCache(s);
}
//get the files directly if we havent found them in the cache, or we arent using it
if (files == null)
{
getFilesFromFolder(s);
}
else
{
try
{
foreach (FileShortInfo f in files)
{
if (addFileToResults(f)) { allFiles.Add(f); }
}
}
catch (Exception e)
{
throw new Exception("Error transferring file to results object", e);
}
}
}
}
catch (Exception e)
{
throw new Exception("Error scanning for files", e);
}
if (showCacheStats)
{
MessageBox.Show(
"Total Time: " + DateTime.Now.Subtract(time_start).TotalSeconds.ToString() + "\n" +
"Files " + files_scanned.ToString() + ", Directories " + dirs_scanned.ToString() + "\n" +
"Files Added: " + allFiles.Count.ToString() + "\n" +
"File Time: " + tim_file.ToString() + "\n" +
"Dir Time: " + tim_dir.ToString() +
(useCache ? "\n" + cache.getStats() : "")
);
}
if (useCache) { cache.saveCache(); }
}
catch (Exception e)
{
throw new Exception("Error during scanDirectories", e);
}
}
public static void pickRandom()
{
//pick a random file from our list
Random r = new Random(DateTime.Now.Millisecond + DateTime.Now.Second + DateTime.Now.Minute);
//are we shuffling, and at the end of the shuffled list?
if (shuffle && allFiles.Count == 0 && pickedFiles.Count > 0)
{
allFiles = pickedFiles;
pickedFiles = new List<FileShortInfo>();
}
if (allFiles.Count > 0)
{
FileShortInfo fi_s = allFiles[r.Next(allFiles.Count)];
FileInfo fi = new FileInfo(fi_s.FullPath);
String overrideApp = isExtensionOverridden(fi.Extension);
ProcessStartInfo startinfo = new ProcessStartInfo();
if (shuffle)
{
pickedFiles.Add(fi_s);
allFiles.Remove(fi_s);
}
if(overrideApp == "")
{
startinfo = new ProcessStartInfo(fi.FullName);
}
else{
startinfo = new ProcessStartInfo(overrideApp, "\"" + fi.FullName + "\"");
}
if (hideSpawnedWindows)
{
startinfo.CreateNoWindow = true;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.UseShellExecute = false;
}
try
{
Process.Start(startinfo);
}
catch (Exception e)
{
MessageBox.Show("Couldnt start program - " + overrideApp + " - " + fi.FullName + " - " + e.Message + " " + e.StackTrace, "Couldnt start program");
}
}
}
public static void pickChoice()
{
Chooser c = new Chooser();
c.ShowDialog();
if (c.DialogResult == DialogResult.OK)
{
openFile(c.result);
}
}
public static void openFile(FileShortInfo fi)
{
String overrideApp = isExtensionOverridden(fi.Extension);
if (overrideApp == "")
{
System.Diagnostics.Process.Start(fi.FileName);
}
else
{
System.Diagnostics.Process.Start(overrideApp, "\"" + fi.FullPath + "\"");
}
}
/// <summary>
/// Scan directories recursively using the Kernel32 shite at the bottom.
/// </summary>
/// <param name="dirName"></param>
static void getFilesFromFolder(String dirName)
{
IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
WIN32_FIND_DATAW findData;
IntPtr findHandle = INVALID_HANDLE_VALUE;
List<FileShortInfo> filesForCache = new List<FileShortInfo>();
try
{
findHandle = FindFirstFileW(dirName + @"\*", out findData);
if (findHandle != INVALID_HANDLE_VALUE)
{
do
{
//ignore the "up" folders
//ignore subfolders (recursion handled above now)
if (findData.cFileName == "."
|| findData.cFileName == ".."
|| PickRandomFile.isDirectoryExcluded(findData.cFileName)
|| (findData.dwFileAttributes & FileAttributes.Directory) != 0)
continue;
string fullpath = dirName + (dirName.EndsWith("\\") ? "" : "\\") + findData.cFileName;
/*/Directory - recurse
if ((findData.dwFileAttributes & FileAttributes.Directory) != 0 && recurse == true)
{
dirs_scanned++;
getFilesFromFolder(fullpath);
}*/
DateTime file_start = DateTime.Now;
files_scanned++;
FileShortInfo f = new FileShortInfo()
{
CreatedDate = FTimeToDateTime(findData.ftCreationTime),
ModifiedDate = FTimeToDateTime(findData.ftLastWriteTime),
FullPath = fullpath,
Extension = fullpath.Substring(fullpath.LastIndexOf(".")),
FileName = fullpath.Substring(fullpath.LastIndexOf("\\") + 1),
Directory = fullpath.Substring(0, fullpath.LastIndexOf("\\")),
Size = findData.nFileSizeLow
};
//add file to results
if (addFileToResults(f))
{
allFiles.Add(f);
filesForCache.Add(f);
}
tim_file += DateTime.Now.Subtract(file_start).TotalSeconds;
}
while (FindNextFile(findHandle, out findData));
//now update the cache
if (useCache) {cache.addCacheData(dirName, PickRandomFile.fileFilter, filesForCache);}
}
}
catch (Exception e)
{
if (!ignoreMissingFolders)
{
MessageBox.Show("Exception when scanning folder" + dirName + "\n" + e.Message + "\n\n" + e.StackTrace + "\n\n" + e.Source);
}
}
finally
{
if (findHandle != INVALID_HANDLE_VALUE) FindClose(findHandle);
}
}
static bool addFileToResults(FileShortInfo f)
{
try
{
if (usingFileTypes)
{
if (PickRandomFile.isExtensionIncluded(f.Extension))
{
return true;
}
}
else
{
return true;
}
}
catch (Exception e)
{
throw new Exception("Error adding file to results", e);
}
return false;
}
/*static void getFilesFromFolder(DirectoryInfo d)
{
try
{
if (!PickRandomFile.isDirectoryExcluded(d.FullName))
{
//loop through any subdirs
if (recurse == true)
{
foreach (DirectoryInfo subdir in d.GetDirectories())
{
//recurse and all all subdirs
try
{
getFilesFromFolder(subdir);
}
catch (UnauthorizedAccessException)
{
//ignore it..
}
}
}
//add files from current folder
FileInfo[] fiArray = d.GetFiles();
if (firstfileonly && fiArray.Length > 0)
{
allFiles.Add(fiArray[0]);
}
else
{
foreach (FileInfo f in fiArray)
{
if (usingFileTypes)
{
if (PickRandomFile.isExtensionIncluded(f.Extension))
{
allFiles.Add(f);
}
}
else
{
allFiles.Add(f);
}
}
}
}
}
catch (DirectoryNotFoundException) { };
}*/
public static void addPath(String folderPath)
{
DirectoryInfo d = new DirectoryInfo(folderPath);
folderList.Add(d);
}
public static void excludePath(String folderPath)
{
DirectoryInfo d = new DirectoryInfo(folderPath);
excludeList.Add(d);
}
public static void addOverride(string ext, string app)
{
if (!ext.StartsWith(".")) ext = "." + ext;
appOverrides.Add(new extAppPair(ext, app));
}
public static void addFileType(String fileType)
{
if (!fileType.StartsWith(".")) fileType = "." + fileType;
fileTypeList.Add(fileType);
}
public static bool isExtensionIncluded(string extension)
{
if (fileTypeList.Contains(extension))
{
return true;
}
else
{
return false;
}
}
public static bool isDirectoryExcluded(string fullname)
{
foreach (DirectoryInfo di in excludeList)
{
if (di.FullName == fullname)
{
return true;
}
}
return false;
}
/// <summary>
/// returns blank string for a non-overridden ext, otherwise app
/// </summary>
/// <param name="ext"></param>
/// <returns></returns>
public static string isExtensionOverridden(string ext)
{
foreach (extAppPair eap in appOverrides)
{
if (eap.extension == ext)
{
return eap.application;
}
}
return "";
}
//Kernel32 wrappers for fast scanning
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr FindFirstFileW(string lpFileName, out WIN32_FIND_DATAW lpFindFileData);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern bool FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATAW lpFindFileData);
[DllImport("kernel32.dll")]
private static extern bool FindClose(IntPtr hFindFile);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct WIN32_FIND_DATAW
{
public FileAttributes dwFileAttributes;
internal System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
internal System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
internal System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public int nFileSizeHigh;
public int nFileSizeLow;
public int dwReserved0;
public int dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
}
private static DateTime FTimeToDateTime(System.Runtime.InteropServices.ComTypes.FILETIME filetime)
{
long highBits = filetime.dwHighDateTime;
highBits = highBits << 32;
return DateTime.FromFileTimeUtc(highBits + (long)filetime.dwLowDateTime);
}
}
[Serializable]
public class FileShortInfo
{
public string FullPath;
public string Extension;
public string FileName;
public string Directory;
public int Size;
public DateTime ModifiedDate;
public DateTime CreatedDate;
}
}