-
Notifications
You must be signed in to change notification settings - Fork 1
/
Processor.cs
75 lines (70 loc) · 2.46 KB
/
Processor.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
using System;
using System.IO;
using System.Collections.Generic;
namespace GR2Renamer
{
public class Processor
{
public struct Processed
{
public string CurrentName;
public string NewName;
public Exception Err;
}
private Queue<string> filepaths = new Queue<string>();
/// <summary>
/// Recurses a directory, queuing all gr2 files for processing.
/// If limit is greater than 0, queues only limit files.
/// Returns number of files queued.
/// </summary>
public int QueueDirectory(string directory, int limit)
{
int queued = 0;
foreach (var filename in Directory.EnumerateFiles(directory, "*.gr2", SearchOption.AllDirectories))
{
queued++;
this.filepaths.Enqueue(filename);
if (limit > 0 && queued > limit)
{
break;
}
}
return queued;
}
/// <summary>
/// Processes the queued filenames, reading them as GR2 data,
/// renaming them to an appropriate name, and returning data about
/// the rename.
/// </summary>
public IEnumerable<Processed> Process(string outputDirectory)
{
while (this.filepaths.Count > 0)
{
string filePath = this.filepaths.Dequeue();
Processed processed = new Processed
{
CurrentName = filePath,
};
try
{
string generatedName = GranReader.GetGR2Name(filePath);
if (generatedName.Length > 0)
{
string originalFilename = Path.GetFileNameWithoutExtension(filePath);
string from = filePath;
processed.NewName = Path.Combine(outputDirectory, $"{generatedName}_{originalFilename}.gr2");
if (processed.CurrentName != processed.NewName)
{
File.Move(processed.CurrentName, processed.NewName);
}
}
}
catch (Exception e)
{
processed.Err = new Exception($"Failed to process {filePath}: {e.Message}");
}
yield return processed;
}
}
}
}