-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
125 lines (109 loc) · 4.76 KB
/
Program.cs
File metadata and controls
125 lines (109 loc) · 4.76 KB
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
using System;
using System.Collections.Generic;
namespace GoogleDriveOrphanFixer
{
class Program
{
private static string restoreFolderId;
private enum Selection
{
RestoreFolder,
Orphan,
Exit
}
static void Main(string[] args)
{
Selection selection = Startup();
Console.WriteLine("\nInitializing...");
DriveHelper drive = new DriveHelper();
DriveFileHelper fileHelper = new DriveFileHelper(drive);
DriveActivityHelper activityHelper = new DriveActivityHelper(drive);
List<DriveItem> filesToReparent = new List<DriveItem>();
do
{
List<string> fileIDs = new List<string>();
switch (selection)
{
case Selection.RestoreFolder:
fileIDs = fileHelper.GetFilesFromFolderById(restoreFolderId); // finds all files that are in the given folder
break;
case Selection.Orphan:
fileIDs = fileHelper.ScanRootDirectory(); // finds all orphans
break;
}
try
{
filesToReparent = activityHelper.FindOrphanParent(fileIDs);
// backup
//System.IO.File.WriteAllText("output.csv", DriveItem.Header() + string.Join("\r\n", (object[])filesToReparent.ToArray()));
fileHelper.MoveOrphanFilesToParent(restoreFolderId, filesToReparent);
}
catch (Exception e) { Console.WriteLine("Error: {0}", e.Message); }
} while (filesToReparent.Count > 0);
Console.Write("All tasks done. Press any key to exit.");
Console.ReadKey();
}
private static Selection Startup()
{
try
{
Console.SetWindowSize(120, 30);
}
catch { } // if not supported on linux
Console.WriteLine("========================================================================================================================");
Console.WriteLine(_Center("Welcome to the Google Drive Orphan Fixer 2.0!"));
Console.WriteLine(_Center("This tool will try restore the original folder structure of shared files that were removed by others and now appear in the root folder."));
Console.WriteLine("\n");
Console.WriteLine(_Center("Please make sure you have your credentials.json file saved in the same folder as this executable file!"));
Console.WriteLine("\n");
Console.WriteLine(_Center("Icon modified from Freepik from www.flaticon.com"));
Console.WriteLine("========================================================================================================================\n");
Console.WriteLine("Please select:\n" +
"1. There are files in my root folder that were shared and are supposed to be somewhere else\n" +
"2. Exit\n");
ConsoleKeyInfo key;
do
{
_ClearLine();
Console.Write("Select: ");
key = Console.ReadKey();
} while (key.KeyChar < '1' || key.KeyChar > '2');
switch (key.KeyChar)
{
//case '1':
// Console.WriteLine("\n\nIn order to identify your files, the id of the folder where you moved the files is required.\n" +
// "Hint: You can obtain the folder id by opening the folder in your browser and looking at the link.\n" +
// "Your link will have the following format: https://drive.google.com/drive/folders/{folder-id}\n\n");
// Console.Write("Please enter your folder id: ");
// restoreFolderId = Console.ReadLine();
// return Selection.RestoreFolder;
case '1':
return Selection.Orphan;
default:
Environment.Exit(0);
return Selection.Exit;
}
}
static int _GetCenterPos(string text)
{
return (Console.WindowWidth / 2) - (text.Length / 2);
}
static string _Center(string text)
{
return _GetSpace(_GetCenterPos(text)) + text;
}
static string _GetSpace(int amount)
{
string ret = "";
for (int i = 0; i < amount; i++)
{
ret += " ";
}
return ret;
}
static void _ClearLine()
{
Console.Write("\r{0}\r", _GetSpace(Console.WindowWidth));
}
}
}