-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
122 lines (102 loc) · 4.69 KB
/
Program.cs
File metadata and controls
122 lines (102 loc) · 4.69 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
using CommandLine;
using SharpCompress.Archives;
using SharpCompress.Common;
using SharpCompress.Readers;
using Spectre.Console;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Windows.Forms;
namespace TapeListing {
internal class Program {
[STAThread]
static void Main(string[] args) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Parser.Default.ParseArguments<Options>(args).WithParsed(options => {
try {
if (!Directory.Exists(options.Drive)) {
Output.Error("Invalid path.");
return;
}
var drive = new DriveInfo(options.Drive);
Output.Print($"Scanning {options.Drive}");
var startTime = DateTime.Now;
var records = new List<FileRecord>();
AnsiConsole.Progress().AutoClear(false).HideCompleted(false).Columns(new ProgressColumn[] {
new SpinnerColumn(),
new TaskDescriptionColumn(),
new ElapsedTimeColumn(),
}).Start(ctx => {
var task = ctx.AddTask("[green]Scanning...[/]", maxValue: 1);
int processed = 0;
foreach (var file in FileHandling.Enumerate(options.Drive)) {
processed++;
task.Description($"[green]Scanning[/] ({processed}) [grey]{Markup.Escape(file)}[/]");
try {
var fi = new FileInfo(file);
var record = new FileRecord {
Path = file.Replace(options.Drive, "").TrimStart('/').TrimStart('\\'),
Size = fi.Length,
SizeHumanized = StringUtils.HumanizeBytes(fi.Length),
ModifiedDate = fi.LastWriteTime,
Attributes = fi.Attributes,
Hash = ""
};
if (!options.NoHash) {
try {
record.Hash = HashUtils.Compute(file);
}
catch {
record.Hash = "-";
}
}
else {
record.Hash = "-";
}
records.Add(record);
if (options.IncludeZips && FileHandling.IsArchive(file)) {
try {
ZipUtils.Process(file, records);
}
catch(Exception ex) {
if(!options.SkipFailToRead) {
Output.Warn($"Failed to read archive: {file}, {ex.Message}");
}
}
}
}
catch {
}
task.Increment(1);
}
task.StopTask();
task.Description($"[green]Scan complete[/] ({processed} files)");
});
Output.Print($"Scan complete, found {records.Count} items.");
Output.Print($"{(DateTime.Now - startTime)} elapsed.");
var output = Output.Build(records, drive);
if (!string.IsNullOrWhiteSpace(options.OutputPath)) {
File.WriteAllText(options.OutputPath, output, Encoding.UTF8);
Output.Print($"Saved to {options.OutputPath}");
}
else if (options.Print) {
Printer.Print(output);
}
else {
Output.Print(output);
}
}
catch (Exception ex) {
Debug.Print(ex.ToString());
Output.Fatal(ex.Message);
}
});
}
}
}