-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDayFile.cs
83 lines (67 loc) · 2.23 KB
/
DayFile.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
using System;
using System.IO;
using System.Linq;
namespace MigrateData3to4
{
static class DayFile
{
private static readonly string inpFilename = Program.Src + Path.DirectorySeparatorChar + "dayfile.txt";
private static readonly string outFilename = Program.Dst + Path.DirectorySeparatorChar + "dayfile.txt";
static internal void Convert()
{
Console.WriteLine($"Migrating v3 dayfile.txt to {outFilename}");
if (File.Exists(inpFilename))
{
Utils.LogMessage($"Dayfile: Found input file: {inpFilename}");
}
else
{
Console.WriteLine("Dayfile: Warning! Input dayfile.txt not found");
Utils.LogMessage("Dayfile: Warning! Input dayfile.txt not found");
return;
}
var lineNum = 1;
try
{
// read the first line to determine format
var lines = File.ReadLines(inpFilename).ToArray();
Program.sepField = Utils.GetLogFileSeparator(lines[0], ',');
Utils.LogMessage($"LogFile: File {inpFilename} is using the field separator: {Program.sepField}");
Program.sepTime = Utils.GetDayFileTimeSeparator(lines[0].Split(Program.sepField)[3], ':');
Utils.LogMessage($"LogFile: File {inpFilename} is using the time separator: {Program.sepTime}");
if (Program.sepTime != ':')
{
for (var i = 0; i < lines.Length; i++)
{
lines[i] = lines[i].Replace(Program.sepTime, ':');
}
}
Utils.TryDetectNewLine(inpFilename, out string endOfLine);
Utils.LogMessage($"LogFile: File {inpFilename} is using the line ending: {(endOfLine == "\n" ? "\\n" : "\\r\\n")}");
using var sw = new StreamWriter(outFilename) { NewLine = endOfLine };
foreach (var inpLine in lines)
{
var fields = inpLine.Split(Program.sepField);
// Do the date
fields[0] = Utils.DdmmyyStrToStr(fields[0]);
// do the rest of the fields, converting comma decimals to dot
for (var i = 1; i < fields.Length; i++)
{
fields[i] = fields[i].Replace(',', '.');
}
// Write the output
sw.WriteLine(string.Join(',', fields));
lineNum++;
}
sw.Flush();
sw.Close();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error at line {lineNum} - {ex.Message}\n");
Console.ResetColor();
}
}
}
}