This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
111 lines (95 loc) · 4.11 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
using CommandLine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WoWUE4CSVConvert
{
class Options
{
[Option('f', "folder", Required = true,
HelpText = "Folder with CSV files.")]
public String SourceFilesPath { get; set; }
[Option('m', "merge", Required = true,
HelpText = "If true, merge all CSVs to one.")]
public bool MergeAllCSV { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Start Progress Time now
DateTime startTime = DateTime.Now;
// Get Command line args
var result = Parser.Default.ParseArguments<Options>(args);
// If no errors occured..
if (!result.Errors.Any())
{
// Get files in source directory
DirectoryInfo sourceDir = new DirectoryInfo(result.Value.SourceFilesPath);
FileInfo[] sourceFiles = sourceDir.GetFiles("*.csv");
// Get the file count and create a counter
int fileMaxCount = sourceFiles.Count();
int fileCount = 0;
// Create folders where processed files are being placed into
Directory.CreateDirectory(Path.Combine(result.Value.SourceFilesPath, "processed"));
bool merge = result.Value.MergeAllCSV;
// Create new Progress Bar
using (var progress = new ProgressBar())
{
List<string> allLines = new List<string>();
var allIndex = 1;
// For every file in the source folder..
foreach (FileInfo importFile in sourceFiles)
{
// Make new index
var lineIndex = 1;
// Read all lines
var lines = File.ReadAllLines(importFile.FullName);
// Loop through them
for (int i = 0; i < lines.Length; i++)
{
// Replace all semicolons with commas
lines[i] = lines[i].Replace(';', ',');
// If it's the first line
if (i == 0)
{
// Add a comma at the start
lines[i] = "," + lines[i];
}
else
{
if (merge)
{
allLines.Add(allIndex + "," + lines[i]);
allIndex++;
}
else
{
lines[i] = lineIndex + "," + lines[i];
lineIndex++;
}
}
}
if(!merge)
{
// Write all lines
File.WriteAllLines(Path.Combine(importFile.DirectoryName, "processed", importFile.Name), lines);
}
// Increase Progress
fileCount++;
TimeSpan timeRemaining = TimeSpan.FromTicks(DateTime.Now.Subtract(startTime).Ticks * (fileMaxCount - (fileCount + 1)) / (fileCount + 1));
progress.timeRemainingText = timeRemaining.ToString("G");
progress.Report((double)fileCount / fileMaxCount);
}
if(merge)
{
File.WriteAllLines(Path.Combine(result.Value.SourceFilesPath, "combined.csv"), allLines);
}
}
}
}
}
}