-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesktopSorterFolders.cs
115 lines (109 loc) · 4.42 KB
/
DesktopSorterFolders.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
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace DesktopSorter
{
class DesktopSorterFolders
{
/// <summary>
/// Tries to move folders from the active desktop
/// to the sorting directory, ignoring folders
/// that are over 1gig in size
/// </summary>
/// <returns>
/// a List of strings of the directories that got moved
/// </returns>
public static List<string> compressFolders()
{
if (DesktopSorterVariables.ignoreExtensions.Contains("folders"))
{
return new List<string>();
}
#region Variable Initializing
List<string> foldersCompressed = new List<string>();
List<string> foldersIgnored = new List<string>();
string[] dirs = Directory.GetDirectories(DesktopSorterVariables.desktopPath);
#endregion
#region Folder Compression Logic
// Loop through each directory
// on our Desktop
foreach (var d in dirs)
{
if (DesktopSorterVariables.foldersToIgnore.Contains(Path.GetFileName(d).ToLower()))
continue;
float size = DesktopSorterUtilities.directorySize(d);
string sizeGB = DesktopSorterUtilities.bytesToString((long)size);
bool folderTooBig = false;
// Check to see if the file
// is over over a gigabyte in size
foreach (var ftb in DesktopSorterVariables.foldersTooBig)
{
if (sizeGB.ToLower().Contains(ftb))
{
folderTooBig = true;
break;
}
}
if (folderTooBig)
{
foldersIgnored.Add(d);
continue;
}
foldersCompressed.Add(d);
}
if (foldersCompressed.Count > 0)
{
if (Directory.Exists(DesktopSorterVariables.desktopPath + "/DesktopSorter/"))
{
if (!Directory.Exists(DesktopSorterVariables.desktopPath + "/DesktopSorter/_DesktopSorterFolders/"))
Directory.CreateDirectory(DesktopSorterVariables.desktopPath + "/DesktopSorter/_DesktopSorterFolders/");
// Moves each folder we stored
// to foldersCompressed, into
// out compression directory
foreach (var f in foldersCompressed)
{
Directory.Move(f, DesktopSorterVariables.desktopPath + "/DesktopSorter/_DesktopSorterFolders/" + Path.GetFileName(f));
}
}
}
#endregion
return foldersCompressed;
}
/// <summary>
/// Tries to decompress the folders (containing files)
/// back to the active desktop
/// </summary>
/// <returns>a List of strings of the directories that got moved</returns>
public static List<string> decompressFolders()
{
List<string> foldersDecompressed = new List<string>();
string[] folders = new string[] { };
try
{
folders = Directory.GetDirectories(DesktopSorterVariables.desktopPath + "/DesktopSorter/_DesktopSorterFolders/");
}
catch
{
return foldersDecompressed;
}
// Loop through each directory
// in our compression directory
foreach (var f in folders)
{
// TODO: May need to wrap this try in a while loop
try
{
// Move the file back to the desktop
Directory.Move(f, DesktopSorterVariables.desktopPath + "/" + Path.GetFileName(f));
}
catch
{
// folder has something in use by user, cannot move
MessageBox.Show("You have a program or file running, the application is paused until it has been closed.");
}
foldersDecompressed.Add(f);
}
return foldersDecompressed;
}
}
}