I have implemented the solution which Cuong suggested here: C# Processing Fixed Width Files
I have also made it go through a folder and apply that to all the .txt files in that folder.
All that works fine, but for some of the .txt files it fails on the var csvLines with the following error:
{"Index and length must refer to a location within the string.\r\nParameter name: length"}
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at FixedWidthFiles.Main.<>c__DisplayClass11.<>c__DisplayClass13.<buttonProcessAllFiles_Click>b__d(KeyValuePair`2 pair) in \\GBMACCMPFS11\Shhk$\Visual Studio 2010\Projects\FixedWidthFiles\FixedWidthFiles\Main.cs:line 138
at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
at System.String.Join(String separator, IEnumerable`1 values)
at FixedWidthFiles.Main.<>c__DisplayClass11.<buttonProcessAllFiles_Click>b__c(String line) in \\GBMACCMPFS11\Shhk$\Visual Studio 2010\Projects\FixedWidthFiles\FixedWidthFiles\Main.cs:line 137
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.IO.File.InternalWriteAllLines(TextWriter writer, IEnumerable`1 contents)
at System.IO.File.WriteAllLines(String path, IEnumerable`1 contents)
at FixedWidthFiles.Main.buttonProcessAllFiles_Click(Object sender, EventArgs e) in \\GBMACCMPFS11\Shhk$\Visual Studio 2010\Projects\FixedWidthFiles\FixedWidthFiles\Main.cs:line 140
Any idea what is wrong? It might be the file, but I am hoping that something can be corrected/improved in the code :)
Code is this:
private void buttonProcessAllFiles_Click(object sender, EventArgs e)
{
if (fileFolderPath == "")
{
MessageBox.Show("Load Folder First", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
int count = 0;
//foreach (var file in Directory.GetFiles(fileFolderPath, "*.txt", SearchOption.AllDirectories))
foreach (var file in Directory.GetFiles(fileFolderPath, "*.txt"))
{
count++;
System.Diagnostics.Debug.WriteLine(count);
fileFolderFull = Path.GetFullPath(file);
System.Diagnostics.Debug.WriteLine(fileFolderFull);
fileFolderName = Path.GetFileNameWithoutExtension(file);
System.Diagnostics.Debug.WriteLine(fileFolderName);
//MessageBox.Show("Full Folder: " + fileFolderFull);
//MessageBox.Show("File Name: " + fileFolderName);
var lines = File.ReadAllLines(fileFolderFull);
var widthList = lines.First().GroupBy(c => c)
.Select(g => g.Count())
.ToList();
var list = new List<KeyValuePair<int, int>>();
int startIndex = 0;
for (int i = 0; i < widthList.Count(); i++)
{
var pair = new KeyValuePair<int, int>(startIndex, widthList[i]);
list.Add(pair);
startIndex += widthList[i];
}
try
{
var csvLines = lines.Select(line => string.Join(",",
list.Select(pair => line.Substring(pair.Key, pair.Value))));
File.WriteAllLines(fileFolderPath + "\\" + fileFolderName + ".csv", csvLines);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
MessageBox.Show("File Saved", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
The line where the error is this:
var csvLines = lines.Select(line => string.Join(",",
list.Select(pair => line.Substring(pair.Key, pair.Value))));