-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
126 lines (106 loc) · 4.42 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dot_clean
{
class Program
{
/// <summary>
/// Program entry point.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.WriteLine("\ndot_clean for .NET v1.0\n(c) 2013 by bagnz0r <http://github.com/bagnz0r>\n\n");
if (args.Length < 1)
{
Console.WriteLine("Usage: dot_clean <path>\n\n");
}
// Take path and verify that it exists.
string path = args[0];
if (Directory.Exists(path) != true)
{
Console.WriteLine("Path doesn't exist.");
Environment.Exit(1);
}
// List all possible junk files here.
Console.WriteLine("Looking for junk files... (Be patient)\n");
List<string> junkFiles = FindJunkFiles(path);
// Spam with some statistical bullshit.
Console.WriteLine("Found " + (junkFiles.Count).ToString() + " junk files!\n\n");
// Delete all 'em suckers.
try
{
DeleteJunkFiles(junkFiles);
}
catch (Exception ex)
{
Console.WriteLine("The program has failed to run!");
File.WriteAllText("error.log", ex.Message + ": " + ex.StackTrace);
}
}
/// <summary>
/// Finds and returns all the junk files possible.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static List<string> FindJunkFiles(string path)
{
List<string> junkFiles = new List<string>();
// Find all the possible junk files that occur in every directory (OS X specific).
AddAllItemsToList(junkFiles, Directory.GetFiles(path, ".com.apple*", SearchOption.AllDirectories));
AddAllItemsToList(junkFiles, Directory.GetFiles(path, "._*", SearchOption.AllDirectories));
AddAllItemsToList(junkFiles, Directory.GetFiles(path, ".DS_Store", SearchOption.AllDirectories));
AddAllItemsToList(junkFiles, Directory.GetFiles(path, ".__MACOSX", SearchOption.AllDirectories));
// Find all the possible junk files that occur in every directory (Windows specific).
AddAllItemsToList(junkFiles, Directory.GetFiles(path, "Thumbs.db", SearchOption.AllDirectories));
// Find all the possible junk folders that occur only in top path (OS X specific).
AddAllItemsToList(junkFiles, Directory.GetFiles(path, ".Spotlight-V100", SearchOption.TopDirectoryOnly));
AddAllItemsToList(junkFiles, Directory.GetFiles(path, ".TemporaryItems", SearchOption.TopDirectoryOnly));
AddAllItemsToList(junkFiles, Directory.GetFiles(path, ".Trashes", SearchOption.TopDirectoryOnly));
AddAllItemsToList(junkFiles, Directory.GetFiles(path, ".fseventsd", SearchOption.AllDirectories));
return junkFiles;
}
/// <summary>
/// Deletes all the junk files and logs the progress.
/// </summary>
/// <param name="junkFiles"></param>
private static void DeleteJunkFiles(List<string> junkFiles)
{
StreamWriter logWriter = new StreamWriter("dot_clean.log");
foreach (string junkFile in junkFiles)
{
if (Directory.Exists(junkFile))
{
Directory.Delete(junkFile);
}
else
{
File.Delete(junkFile);
}
string removedMsg = "Removed '" + junkFile + "'...";
Console.WriteLine(removedMsg + "\n");
logWriter.WriteLine(removedMsg);
}
logWriter.Flush();
logWriter.Dispose();
}
/// <summary>
/// Loops over all the items in an array and adds them to referenced list.
/// </summary>
/// <param name="list"></param>
/// <param name="items"></param>
/// <returns></returns>
private static List<string> AddAllItemsToList(List<string> list, string[] items)
{
foreach (string item in items)
{
list.Add(item);
}
return list;
}
}
}