forked from SvenGroot/Ookii.BinarySize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
48 lines (42 loc) · 1.18 KB
/
Program.cs
File metadata and controls
48 lines (42 loc) · 1.18 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
using ListDirectory;
using Ookii;
// Parse the command line arguments using Ookii.CommandLine.
var arguments = Arguments.Parse();
if (arguments == null)
{
return 1;
}
// Enumerate the files, and convert the info to a temporary format that uses BinarySize for
// convenience.
var files = arguments.Path.EnumerateFiles()
.Select(f => new { f.Name, Size = (BinarySize)f.Length });
// Filter by minimum size, if the user used that option.
if (arguments.MinSize is BinarySize minSize)
{
files = files.Where(f => f.Size >= minSize);
}
// List every file, and display the size using the shortest possible representation (the largest
// possible unit, using fractional values where necessary).
foreach (var file in files)
{
if (arguments.SiUnits)
{
Console.WriteLine($"{file.Name}: {file.Size:0.# sB}");
}
else
{
Console.WriteLine($"{file.Name}: {file.Size:0.# SiB}");
}
}
// Use the Sum extension method provided for BinarySize to add up all the values.
var total = files.Sum(f => f.Size);
Console.WriteLine();
if (arguments.SiUnits)
{
Console.WriteLine($"Total: {total:0.# sB}");
}
else
{
Console.WriteLine($"Total: {total:0.# SiB}");
}
return 0;