-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesktopSorterFiles.cs
221 lines (213 loc) · 10 KB
/
DesktopSorterFiles.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace DesktopSorter
{
/// <summary>
/// Class to handle the moving of files
/// from and to the desktop directory
/// </summary>
class DesktopSorterFiles
{
/// <summary>
/// Attempts to move the files from
/// the desktopSorter directory
/// </summary>
/// <returns>List of files decompressed</returns>
public static List<string> decompressFiles()
{
#region Public Desktop Decompression Logic
bool publicAccessError = false;
List<string> p_filesDecompressed = new List<string>() { };
try
{
string[] p_directories = Directory.GetDirectories(DesktopSorterVariables.desktopPath + "/DesktopSorter/_PublicDesktop");
// Loop through each directory inside of
// our DesktopSorter/_PublicDesktop Folder
foreach (var dir in p_directories)
{
string[] files = Directory.GetFiles(dir + "/");
// Loop through each file in
// the current indexed directory
foreach (var f in files)
{
if (DesktopSorterVariables.ignoreFiles.Contains(Path.GetFileName(f).ToLower()))
continue;
// This try attempt will fail if the user
// isn't running CMD as admin and then calling
// this program via the terminal. If they
// aren't running CMD as admin, we can not
// move Files 'back' to User/Public/Desktop <- protected
// but can move them 'from' that location with
// no problem, weirdly enough..
try
{
File.Move(f, DesktopSorterVariables.desktopPublicPath + "/" + Path.GetFileName(f));
p_filesDecompressed.Add(f);
}
catch
{
foreach(var errorline in DesktopSorterVariables.errorAccessDenied)
Console.WriteLine(errorline);
publicAccessError = true;
break;
}
}
if (publicAccessError)
break;
}
}
catch
{
Console.WriteLine("No such directory for _PublicDesktop, ignoring execution logic.");
}
#endregion
#region User Desktop Decompression Logic
List<string> filesDecompressed = new List<string>() { };
try
{
string[] directories = Directory.GetDirectories(DesktopSorterVariables.desktopPath + "/DesktopSorter/");
// Loop through each directory inside of
// our DesktopSorter/_PublicDesktop Folder
foreach (var dir in directories)
{
string[] files = Directory.GetFiles(dir + "/");
// Loop through each file in
// the current indexed directory
foreach (var f in files)
{
if (Path.GetFileName(f).Contains("DesktopSorter"))
continue;
File.Move(f, DesktopSorterVariables.desktopPath + "/" + Path.GetFileName(f));
filesDecompressed.Add(f);
}
}
// If user isn't running CMD as admin
// then we'll move files back to the
// normal desktop, in a folder named:
// _PublicDesktop. These were files
// stored on the users Public Desktop
// Directory, which requires Admin Access
if (publicAccessError)
Directory.Move(DesktopSorterVariables.desktopPath + "/DesktopSorter/_PublicDesktop/", DesktopSorterVariables.desktopPath + "/_PublicDesktop/");
Directory.Delete(DesktopSorterVariables.desktopPath + "/DesktopSorter/", true);
filesDecompressed.AddRange(p_filesDecompressed);
}
catch
{
Console.WriteLine("No such directory for DesktopSorter, ignoring execution logic.");
}
#endregion
filesDecompressed.AddRange(p_filesDecompressed);
return filesDecompressed;
}
/// <summary>
/// Attempts to move the files to
/// the desktopSorter directory
/// </summary>
/// <returns>List of files compressed</returns>
public static List<string> compressFiles()
{
Directory.CreateDirectory(DesktopSorterVariables.desktopPath + "/DesktopSorter");
Directory.CreateDirectory(DesktopSorterVariables.desktopPath + "/DesktopSorter/_PublicDesktop");
string[] files = Directory.GetFiles(DesktopSorterVariables.desktopPath);
List<string> folders = new List<string>();
#region User Desktop Compression Logic
// Loop through all files on the Desktop
// And append their extension to a list
// (if not on the list already)
foreach (var f in files)
{
if (DesktopSorterVariables.ignoreFiles.Contains(Path.GetFileName(f).ToLower()) || DesktopSorterVariables.ignoreExtensions.Contains(Path.GetExtension(f).ToLower()))
continue;
string ext = Path.GetExtension(f).Replace(".", "").Trim();
if (!folders.Contains(ext))
folders.Add(ext);
}
// Create each extension directory
// inside of our DesktopSorter folder
foreach (var f in folders)
{
Directory.CreateDirectory(DesktopSorterVariables.desktopPath + "/DesktopSorter/" + f);
}
// Loop through our files once more
// then on our current indexed file
// Loop through our created extensions to
// check to see if the extension matches
// with an extension directory, and move it there
foreach (var f in files)
{
if (DesktopSorterVariables.ignoreFiles.Contains(Path.GetFileName(f).ToLower()) || DesktopSorterVariables.ignoreExtensions.Contains(Path.GetExtension(f).ToLower()))
continue;
foreach (var _f in folders)
{
if (Path.GetExtension(f).Replace(".", "").Trim() == _f)
{
try
{
File.Move(f, DesktopSorterVariables.desktopPath + "/DesktopSorter/" + _f + "/" + Path.GetFileName(f));
}
catch
{
// file in use by the user, so we can't move it
}
break;
}
}
}
#endregion
#region Public Desktop Compression Logic
string[] files2 = Directory.GetFiles(DesktopSorterVariables.desktopPublicPath);
// Loop through all of our files
// in the Public Desktop Directory,
// create directories and move
// the file accordingly
foreach (var f in files2)
{
if (DesktopSorterVariables.ignoreFiles.Contains(Path.GetFileName(f).ToLower()) || DesktopSorterVariables.ignoreExtensions.Contains(Path.GetExtension(f).ToLower()))
continue;
string file_name = Path.GetFileName(f);
string file_extension = Path.GetExtension(f).Replace(".", "").Trim();
Directory.CreateDirectory(DesktopSorterVariables.desktopPath + "/DesktopSorter/_PublicDesktop/" + file_extension);
try
{
File.Move(f, DesktopSorterVariables.desktopPath + "/DesktopSorter/_PublicDesktop/" + file_extension + "/" + file_name);
}
catch
{
// file in use by the user (in _PublicDesktop), so we cannot move it
}
}
#endregion
#region Public Desktop Access denied folder check
if (Directory.Exists(DesktopSorterVariables.desktopPath + "/_PublicDesktop/"))
{
// Make sure the _PublicDesktop directory is valid
if (Directory.Exists(DesktopSorterVariables.desktopPath + "/DesktopSorter/_PublicDesktop/"))
{
string[] pd_folders = Directory.GetDirectories(DesktopSorterVariables.desktopPath + "/_PublicDesktop/");
bool pd_fail = false;
foreach (var pd_f in pd_folders)
{
try
{
Directory.Move(pd_f, DesktopSorterVariables.desktopPath + "/DesktopSorter/_PublicDesktop/" + Path.GetFileName(pd_f));
}
catch
{
pd_fail = true;
break;
}
}
if (!pd_fail && pd_folders.Length > 0)
Directory.Delete(DesktopSorterVariables.desktopPath + "/_PublicDesktop");
}
}
#endregion
List<string> files_return = new List<string>(files);
files_return.AddRange(files2);
return files_return;
}
}
}