-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
158 lines (126 loc) · 3.96 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
// Copyright © 2015, EPSITEC SA, CH-1400 Yverdon-les-Bains, Switzerland
// Author: Pierre ARNAUD, Maintainer: Pierre ARNAUD
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace RecursiveDelete
{
public class Program
{
static void Main(string[] args)
{
if (args.Length != 1)
{
var programName = System.IO.Path.GetFileName (System.Environment.GetCommandLineArgs ()[0]);
System.Console.Error.WriteLine ("Specify the path of a directory and it will be deleted recursively.");
System.Console.Error.WriteLine ("You can also drop a folder on the program to delete it.");
System.Console.Error.WriteLine ();
System.Console.Error.WriteLine ("Usage:");
System.Console.Error.WriteLine (" {0} \"path to folder\"", programName);
System.Console.Error.WriteLine ();
System.Console.Error.WriteLine ("No confirmation will be asked. The folder, all its files and subfolders");
System.Console.Error.WriteLine ("will be deleted, not moved to the trash. The operation is irreversible.");
System.Console.ReadLine ();
System.Environment.Exit (1);
}
var path = System.IO.Path.GetFullPath (args[0]);
Program.Delete (path, path);
System.Console.WriteLine ();
System.Console.WriteLine ("Longest path found had {0} characters", Program.maxPath.Length);
System.Console.WriteLine (Program.maxPath);
}
private static void Delete(string path, string displayPath)
{
var fullDisplayPath = displayPath;
if (fullDisplayPath.Length > Program.maxPath.Length)
{
Program.maxPath = fullDisplayPath;
}
if (displayPath.Length > 64)
{
displayPath = "..." + displayPath.Substring (displayPath.Length-61, 61);
}
if (Program.ExecuteOperation (() => System.IO.File.Exists (path)))
{
System.Console.WriteLine ("Delete file {0}", displayPath);
Program.DeleteFile (path);
return;
}
if (Program.ExecuteOperation (() => System.IO.Directory.Exists (path)))
{
var shortPath = Program.ShortenDir (path);
System.Console.WriteLine ("Analyzing {0}", displayPath);
var entries = Program.ExecuteOperation (() => System.IO.Directory.GetFileSystemEntries (shortPath));
if (entries != null)
{
foreach (var entry in entries)
{
Program.Delete (entry, System.IO.Path.Combine (fullDisplayPath, System.IO.Path.GetFileName (entry)));
}
}
System.Console.WriteLine ("Delete dir {0}", displayPath);
Program.DeleteDir (shortPath);
}
}
private static void DeleteFile(string path)
{
Program.ExecuteOperation (() => System.IO.File.Delete (path));
}
private static void DeleteDir(string path)
{
Program.ExecuteOperation (() => System.IO.Directory.Delete (path));
}
private static T ExecuteOperation<T>(System.Func<T> operation)
{
T result = default (T);
Program.ExecuteOperation (() =>
{
result = operation ();
});
return result;
}
private static void ExecuteOperation(System.Action operation)
{
try
{
operation ();
}
catch (System.IO.IOException ex)
{
Program.Display (ex);
}
catch (System.UnauthorizedAccessException ex)
{
Program.Display (ex);
}
}
private static void Display(System.Exception ex)
{
var color = System.Console.ForegroundColor;
System.Console.ForegroundColor = System.ConsoleColor.Red;
System.Console.Error.WriteLine (ex.Message);
System.Console.ForegroundColor = color;
}
private static string ShortenDir(string path)
{
var root = System.IO.Path.GetDirectoryName (path);
var name = System.IO.Path.GetFileName (path);
var oldPath = System.IO.Path.Combine (root, name);
var newPath = System.IO.Path.Combine (root, "@");
if (oldPath != newPath)
{
try
{
System.IO.Directory.Move (oldPath, newPath);
}
catch (System.IO.IOException ex)
{
System.Console.WriteLine (ex.Message);
return oldPath;
}
}
return newPath;
}
static string maxPath = "";
}
}