This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
167 lines (142 loc) · 6.17 KB
/
Program.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
using System.Diagnostics;
namespace ExtGet
{
public partial class RecursiveFileProcessor
{
private static readonly string Name = "ExtGet";
private static readonly string Version = "v0.6a BETA";
public static void Main(string[] args)
{
InitiateTracer();
if (args.Length == 0)
{
Trace.WriteLine($"\n{Name} {Version}\n");
Trace.WriteLine("you should use the program with a path argument.\n");
Trace.WriteLine("eg: ExtGet.exe <path>\n");
}
else
{
for (int i = 0; i < args.Length; i++)
{
string path = args[i];
if (File.Exists(path) || Directory.Exists(path))
{
// this path is a file or directory
ProcessFiles(path);
}
else
{
Trace.WriteLine($"{path} is not a valid file or directory.");
}
}
}
}
// initiates a tracer which will print to both
// the Console and to a log file, log.txt
private static void InitiateTracer()
{
string logfile = "log.txt";
if (File.Exists(logfile))
{
try
{
File.Delete(logfile);
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
}
}
Trace.Listeners.Clear();
_ = AppDomain.CurrentDomain.BaseDirectory;
TextWriterTraceListener? twtl = new(logfile)
{
Name = "TextLogger",
TraceOutputOptions = TraceOptions.ThreadId | TraceOptions.DateTime
};
ConsoleTraceListener? ctl = new(false) { TraceOutputOptions = TraceOptions.DateTime };
_ = Trace.Listeners.Add(twtl);
_ = Trace.Listeners.Add(ctl);
Trace.AutoFlush = true;
}
private static long GetDirectorySize(string path)
{
IEnumerable<string>? files = Directory.EnumerateFiles(path);
// get the sizeof all files in the current directory
long currentSize = (from file in files let fileInfo = new FileInfo(file) select fileInfo.Length).Sum();
IEnumerable<string>? directories = Directory.EnumerateDirectories(path);
// get the size of all files in all subdirectories
long subDirSize = (from directory in directories select GetDirectorySize(directory)).Sum();
return currentSize + subDirSize;
}
public static void ProcessFiles(string targetDirectory)
{
// count all extensions in directory and sub directory
Dictionary<string, FileStatisticInfo> extensions = new();
void CalcFilesCount(string targetDirectory)
{
IEnumerable<IGrouping<string, string>>? files = Directory.GetFiles(targetDirectory).GroupBy(p => Path.GetExtension(p));
foreach (IGrouping<string, string>? ex in files)
{
if (extensions.ContainsKey(ex.Key))
{
extensions[ex.Key].Count += ex.Count();
extensions[ex.Key].TotalSize += ex.Sum(filePath => new FileInfo(filePath).Length);
}
else
{
extensions[ex.Key] = new FileStatisticInfo
{
Count = ex.Count(),
TotalSize = ex.Sum(filePath => new FileInfo(filePath).Length)
};
}
}
string[] array = Directory.GetDirectories(targetDirectory);
for (int i = 0; i < array.Length; i++)
{
string? p2 = array[i];
CalcFilesCount(p2);
}
}
CalcFilesCount(targetDirectory);
// convert bytes to more human readable style
static string BytesToString(long byteCount)
{
string[] suf = { " B", " KB", " MB", " GB", " TB", " PB", " EB" };
if (byteCount == 0)
{
return "0" + suf[0];
}
long bytes = Math.Abs(byteCount);
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
return (Math.Sign(byteCount) * num).ToString() + suf[place];
}
// count all the files from directory and sub directory
int fCount = Directory.GetFiles(targetDirectory, "*", SearchOption.AllDirectories).Length;
// print out the results
Trace.WriteLine($"\n{Name} {Version}");
Trace.WriteLine($"- coded by danthespal aka dannybest\n");
Trace.WriteLine($"Target: \"{targetDirectory}\"");
Trace.WriteLine($"Upload data info:\n");
foreach (KeyValuePair<string, FileStatisticInfo> items in extensions)
{
double percentage = (double)Math.Round((double)(items.Value.Count * 100) / fCount, 2);
if (items.Key == "")
{
Trace.WriteLine($"----------.unknown - {percentage}%");
Trace.WriteLine($".unknown : {items.Value.Count:N0} files | {BytesToString(items.Value.TotalSize)}");
Trace.WriteLine("------------------------------\n");
}
else
{
Trace.WriteLine($"----------{items.Key} - {percentage}%");
Trace.WriteLine($"{items.Key} : {items.Value.Count:N0} files | {BytesToString(items.Value.TotalSize)}");
Trace.WriteLine("------------------------------\n");
}
}
Trace.WriteLine($"Total Files: {fCount:N0} | {BytesToString(GetDirectorySize(targetDirectory))}");
}
}
}