-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
185 lines (147 loc) · 6.97 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
using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Linq;
namespace OfficeBatchProcess
{
class ProcessFileThreadState
{
public string FromFile, ToFile;
public ProcessFileThreadState(string FromFileName, string ToFileName)
{
FromFile = FromFileName;
ToFile = ToFileName;
}
}
class Program
{
static void ProcessFile(Object state)
{
try
{
WorkThreadLimit.WaitOne();
using (FileProcessor x = new FileProcessor())
{
ProcessFileThreadState s = (state as ProcessFileThreadState);
if (s.FromFile.EndsWith(".pdf"))
{
/*
byte[] DOCXBuffer = Convert(s.FromFile);
string DOCXSource = s.FromFile.Substring(0, s.FromFile.LastIndexOf(".")) + ".docx";
Interlocked.Add(ref FilesCount, 1);
Interlocked.Add(ref ChangesCount, x.ProcessFile(DOCXBuffer, DOCXSource, s.ToFile));
*/
} else
{
Interlocked.Add(ref FilesCount, 1);
Interlocked.Add(ref ChangesCount, x.ProcessFile(s.FromFile, s.ToFile));
}
}
}
finally
{
WorkThreadLimit.Release();
finished.Signal();
}
}
static int ChangesCount = 0;
static int FilesCount = 0;
static CountdownEvent finished = new CountdownEvent(1);
static System.Threading.Semaphore WorkThreadLimit;
static string SourceDirectory;
static string DestDirectory;
static void WalkDirectoryTree(System.IO.DirectoryInfo root)
{
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
// First, process all the files directly under this folder
try
{
files = (System.IO.FileInfo[])root.GetFiles("*.*").Where(s => s.Name.EndsWith(".xlsx") || s.Name.EndsWith(".docx")).ToArray();
}
// This is thrown if even one of the files requires permissions greater
// than the application provides.
catch (UnauthorizedAccessException e)
{
// This code just writes out the message and continues to recurse.
// You may decide to do something different here. For example, you
// can try to elevate your privileges and access the file again.
Console.WriteLine(e.Message);
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
}
if (files != null)
{
foreach (System.IO.FileInfo fi in files)
{
string DestFileName = DestDirectory + fi.FullName.Substring(fi.FullName.IndexOf(SourceDirectory) + SourceDirectory.Length);
//DestFileName = DestFileName.Substring(0, DestFileName.LastIndexOf(".")) + ".xlsx";
if (File.Exists(DestFileName))
{
Console.WriteLine("File {0} already exists, skipping", DestFileName);
continue;
}
string ExtractsFileName = DestFileName + " - extracts.xml";
if (File.Exists(ExtractsFileName))
{
Console.WriteLine("File {0} already exists, skipping", ExtractsFileName);
continue;
}
finished.AddCount();
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ProcessFile), new ProcessFileThreadState(fi.FullName, DestFileName));
//ChangesCount += x.ProcessFile(fi.FullName, );
}
// Now find all the subdirectories under this directory.
subDirs = root.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
// Resursive call for each subdirectory.
WalkDirectoryTree(dirInfo);
}
}
}
[STAThread]
static void Main(string[] args)
{
if (args.GetLength(0) == 2)
using (FileProcessor x = new FileProcessor())
{
ChangesCount = x.ProcessFile(args[0], args[1]);
return;
}
Console.WriteLine("Automatic Office files update. Syntax: " + Assembly.GetExecutingAssembly().GetName().Name + " <source> <destination>");
Console.WriteLine("If <source> and <destination> are not specified, the program will convert all *.xlsx files from SourceDirectory to DestinationDirectory as specified in app.config file");
Console.WriteLine("Processing with a maximum of " +ConfigurationManager.AppSettings["MaxThreads"] + " threads.");
System.Threading.ThreadPool.SetMaxThreads(int.Parse(ConfigurationManager.AppSettings["MaxThreads"]), 0);
WorkThreadLimit = new System.Threading.Semaphore(int.Parse(ConfigurationManager.AppSettings["MaxThreads"]), int.Parse(ConfigurationManager.AppSettings["MaxThreads"]));
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
if (ConfigurationManager.AppSettings["SourceDirectory"] == null)
{
dialog.Title = "Select a folder with the source XLSX files";
dialog.IsFolderPicker = true;
dialog.InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (dialog.ShowDialog() == CommonFileDialogResult.Ok) SourceDirectory = dialog.FileName; else return;
}
if (ConfigurationManager.AppSettings["DestinationDirectory"] == null)
{
dialog.Title = "Select the destination folder to store processed files";
dialog.IsFolderPicker = true;
dialog.InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (dialog.ShowDialog() == CommonFileDialogResult.Ok) DestDirectory = dialog.FileName; else return;
}
DirectoryInfo rootDir = new DirectoryInfo(SourceDirectory);
WalkDirectoryTree(rootDir);
finished.Signal();
finished.Wait();
Console.WriteLine(ChangesCount.ToString() + " change(s) made total.");
Console.WriteLine(FilesCount.ToString() + " files processed.");
Console.WriteLine("[{0}] Finished", DateTime.Now);
Console.ReadLine();
}
}
}