-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
166 lines (156 loc) · 6.04 KB
/
Program.cs
File metadata and controls
166 lines (156 loc) · 6.04 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
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
/* Simple application to recursively replace all hyperlinks in every docx file found within a folder structure
* the program reads the targets from targets.txt
*
* targets.txt must be in the format:
* https://www.bing.com.au,https://www.google.com.au
*
* The above will replace all instances of bing with google.
*
* Depends on Aspose https://www.aspose.com/
* A valid aspose liscense is required and not included
*/
using Aspose.Words;
using Aspose.Words.Fields;
using FindAndReplaceHyperlink.model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace FindAndReplaceHyperlink
{
class Program
{
static void Main(string[] args)
{
//Set Aspose Lisence
SetLicense();
args = new string[1];
args[0] = "-replace";
if (args.Count() == 0 ||
(args[0].ToLower() != "-report") ||
(args[0].ToLower() != "-replace"))
{
//-report will only output potential matches to a excel file, -replace will update file
Console.WriteLine("must include -report or -replace");
}
else
{
var reportOnly = args[0].ToLower() == "-report";
var rootFolder = AppDomain.CurrentDomain.BaseDirectory;
//get replace target from targets.txt and populate into a List<string>
var targetList = ReadTarget();
var stringList = new List<string>();
var output = SearchAllFiles(ReplaceModel.Convert(targetList), reportOnly, stringList, rootFolder);
File.WriteAllLines("output.csv", stringList.ToArray());
}
}
//Set License for Aspose Word.
//More information here https://docs.aspose.com/display/wordsnet/Licensing
private static void SetLicense()
{
new License().SetLicense("license/aspose.words.lic");
}
/// <summary>
/// Reard targets.txt and return as a List<string>
/// targets.txt should be in the format:
/// oldhyperlink,newhyperlink eg:
/// https://www.bing.com.au,https://www.google.com.au
/// </string>
/// </summary>
/// <returns></returns>
private static IEnumerable<string> ReadTarget()
{
try
{
return File.ReadAllLines("targets.txt");
}
catch (Exception ex)
{
Console.WriteLine("Error", ex.ToString());
}
throw new FileNotFoundException("target.txt must exist in folder");
}
/// <summary>
/// Recursively search all files and folders within the director
/// </summary>
/// <param name="replaceTargetList"></param>
/// <param name="reportOnly"></param>
/// <param name="stringList"></param>
/// <param name="folder"></param>
/// <returns></returns>
private static List<string> SearchAllFiles(List<ReplaceModel> replaceTargetList,
bool reportOnly,
List<string> stringList,
string folder)
{
Console.WriteLine($"Searching {folder}");
//Search all folders recursively
var folders = Directory.GetDirectories(folder);
foreach (var newFolder in folders)
{
stringList = SearchAllFiles(replaceTargetList, reportOnly, stringList, newFolder);
}
string[] files = Directory.GetFiles(folder, "*.doc*");
foreach (string file in files)
{
Console.WriteLine("Searching " + file);
Document document = null;
//Load DOCX using aspose
if (GetDocument(file, out document))
{
//itterate throguh all "fields"
//More informatio here https://docs.aspose.com/display/wordsnet/Working+with+Fields
foreach (Field field in document.Range.Fields)
{
//Only work on Hyperlinks
if (field.Type == FieldType.FieldHyperlink)
{
Console.WriteLine("Found");
var link = (FieldHyperlink)field;
var replaceModel = replaceTargetList
.Where<ReplaceModel>((Func<ReplaceModel, bool>)(a => link.Address.ToLower()
.Contains(a.From.ToLower())))
.FirstOrDefault<ReplaceModel>();
if (replaceModel != null)
{
var newAddress = Regex.Replace(link.Address, replaceModel.From, replaceModel.To, RegexOptions.IgnoreCase);
var outputLine = file + "," + link.Address + "," + newAddress;
stringList.Add(outputLine);
if (!reportOnly)
link.Address = newAddress;
}
}
}
//Save updated docx with Aspose
if (!reportOnly)
{
document.Save(file);
}
}
}
return stringList;
}
/// <summary>
/// Read Docx
/// </summary>
/// <param name="file"></param>
/// <param name="document"></param>
/// <returns></returns>
private static bool GetDocument(string file, out Document document)
{
document = (Document)null;
try
{
document = new Document(file);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.ToString());
}
return document != null;
}
}
}