-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
199 lines (165 loc) · 8.66 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
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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using CommandLine;
using Microsoft.Office.Interop.Word;
namespace OfficeFileVersionUpdater;
internal class Program
{
private static string _folderToParse;
private static bool _skipWord, _skipExcel, _skipPowerPoint;
private static void Main(string[] args)
{
Console.WriteLine(
value:
"\u001b[2J\u001b[3J"); // cls -> https://www.reddit.com/r/csharp/comments/k8flpr/comment/gextslz/?utm_source=share&utm_medium=web2x&context=3
Console.Clear();
ParseArgs(args: args);
string[] wordFiles = { }, excelFiles = { }, powerPointFiles = { };
CollateFiles(folderToParse: _folderToParse);
string officeVer = ProgramHelpers.GetOfficeVer(); // this is used for blacklist-clearing.
if (wordFiles.Length > 0 && !_skipWord)
{
Console.WriteLine(value: "Starting Word files...");
ProgramHelpers.ClearBlackList(whichApp: "Word", officeVer: officeVer);
WordHandler wordUpdater = new();
Application wordApp = wordUpdater.StartApp();
foreach (string wordFile in wordFiles)
wordUpdater.ProcessAndSaveFile(wordDoc: wordUpdater.OpenFile(fileNameWithPath: wordFile,
wordApp: wordApp));
wordUpdater.QuitApp();
}
if (excelFiles.Length > 0 && !_skipExcel)
{
Console.WriteLine(value: "Starting Excel files...");
ProgramHelpers.ClearBlackList(whichApp: "Excel", officeVer: officeVer);
ExcelHandler excelUpdater = new();
Microsoft.Office.Interop.Excel.Application excelApp = excelUpdater.StartApp();
foreach (string excelFile in excelFiles)
excelUpdater.ProcessAndSaveFile(excelWbk: excelUpdater.OpenFile(fileNameWithPath: excelFile,
excelApp: excelApp));
excelUpdater.QuitApp();
}
if (powerPointFiles.Length > 0 && !_skipPowerPoint)
{
Console.WriteLine(value: "Starting PowerPoint files...");
ProgramHelpers.ClearBlackList(whichApp: "PowerPoint", officeVer: officeVer);
PowerPointHandler powerPointUpdater = new();
Microsoft.Office.Interop.PowerPoint.Application powerPointApp = powerPointUpdater.StartApp();
foreach (string powerPointFile in powerPointFiles)
powerPointUpdater.ProcessAndSaveFile(powerPointPres: powerPointUpdater.OpenFile(
fileNameWithPath: powerPointFile,
powerPointApp: powerPointApp));
powerPointUpdater.QuitApp();
}
ProgramHelpers.ExitWithMessage(exitReason: ExitReasons.Ok);
void CollateFiles(string folderToParse)
{
if (!_skipWord)
{
Console.WriteLine(value: "Starting file collation in root of " + folderToParse);
wordFiles = Directory.EnumerateFiles(path: folderToParse, searchPattern: "*.*",
searchOption: SearchOption.AllDirectories)
.Where(predicate: s => s.ToLower()
.EndsWith(value: ".doc") ||
s.ToLower()
.EndsWith(value: ".docx") ||
s.ToLower()
.EndsWith(value: ".docm"))
.ToArray();
Console.WriteLine(value: $"{wordFiles.Length} Word files.");
}
// Excel files don't seem to have versionings. They're either "old" or "new".
// ...as such we don't need the x-files.
if (!_skipExcel)
{
excelFiles = Directory.EnumerateFiles(path: folderToParse, searchPattern: "*.*",
searchOption: SearchOption.AllDirectories)
.Where(predicate: s => s.ToLower()
.EndsWith(value: ".xls"))
.ToArray();
Console.WriteLine(value: $"{excelFiles.Length} Excel files.");
}
// PowerPoint files don't seem to have versionings. They're either "old" or "new".
// ...as such we don't need the x-files.
if (!_skipPowerPoint)
{
powerPointFiles = Directory.EnumerateFiles(path: folderToParse, searchPattern: "*.*",
searchOption: SearchOption.AllDirectories)
.Where(predicate: s => s.ToLower()
.EndsWith(value: ".ppt") ||
s.ToLower()
.EndsWith(value: ".pps"))
.ToArray();
Console.WriteLine(value: $"{powerPointFiles.Length} PowerPoint files.");
}
Console.WriteLine(value: "File collation done.");
if (wordFiles.Length + excelFiles.Length + powerPointFiles.Length == 0)
{
Console.WriteLine(value: "Nothing to do.");
ProgramHelpers.ExitWithMessage(exitReason: ExitReasons.Ok);
}
}
}
/// <summary>
/// This is also responsible for parsing the program arguments/parameters
/// </summary>
/// <param name="args"></param>
private static void ParseArgs(string[] args)
{
Parser parser = new(configuration: settings => { settings.CaseSensitive = false; });
ParserResult<Options> parseResult = parser.ParseArguments<Options>(args: args)
.WithParsed(action: o =>
{
_folderToParse =
o.FolderToParse.Replace(oldValue: "\"", newValue: "");
if (!Directory.Exists(path: _folderToParse))
ProgramHelpers.ExitWithMessage(
exitReason: ExitReasons.InvalidFolder);
_skipWord = o.SkipWord;
_skipExcel = o.SkipExcel;
_skipPowerPoint = o.SkipPowerPoint;
}
);
foreach (Error parseError in parseResult.Errors) Console.WriteLine(value: parseError);
if (string.IsNullOrWhiteSpace(value: _folderToParse))
{
ProgramHelpers.ExitWithMessage(exitReason: parseResult.Errors.Any()
? ExitReasons.InvalidParametersSupplied
: ExitReasons.NoFolderPassed); // bye bye
// bye bye
}
}
/// <summary>
/// List of Exit Reasons/Codes
/// </summary>
internal enum ExitReasons
{
Ok,
NoFolderPassed,
InvalidFolder,
WordNotInstalled,
ExcelNotInstalled,
PowerpointNotInstalled,
InvalidParametersSupplied
}
/// <summary>
/// This is responsible for parsing the program arguments/parameters
/// </summary>
[SuppressMessage(category: "ReSharper", checkId: "UnusedAutoPropertyAccessor.Local")]
private class Options
{
[Option(shortName: 'f', longName: "folderToParse", Required = true,
HelpText =
"""Folder to parse -- this is recursive so you only need to specify the top-level. Use double-quotes if the folder name contains spaces (e.g. OfficeFileVersionUpdater.exe -f "C:\someting something")""")]
public string FolderToParse { get; set; }
[Option(longName: "SkipWord", Required = false, HelpText = "Do NOT Parse Word files.")]
public bool SkipWord { get; set; }
[Option(longName: "SkipExcel", Required = false, HelpText = "Do NOT Parse Excel files.")]
public bool SkipExcel { get; set; }
[Option(longName: "SkipPowerPoint", Required = false, HelpText = "Do NOT Parse PowerPoint files.")]
public bool SkipPowerPoint { get; set; }
}
}