Saving Specific Lines to New Text Files - c#

I am busy working with text files that are structured as seen below:
This is rainfall data in a continuous string each 5 characters after the date represent a day in the month.
0005880 W 1926 9-7777-7777-7777-7777-7777-7777-7777-7777-7777 117 130 64-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777
0005880 W 192610-7777-7777-7777-7777-7777-7777-7777-7777-7777 23-7777-7777-7777-7777 3-7777 226 462 71-7777-7777 157 76 15-7777-7777-7777-7777-7777-7777-7777
0005880 W 192611 3 20-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777 61 142-7777-7777-7777 8-7777-7777-7777-7777
0005880 W 192612-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777 132-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777
The year and month are represented in (10, 4) and (14, 2) positions within the string.
My problem is that there is instances when the next line isn't the month that is meant to follow. I have written code that adds a line where a month of data is missing shown bellow.
public void findGapsToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage tp = new TabPage();
RichTextBox rtb = new RichTextBox();
rtb.Dock = DockStyle.Fill;
rtb.Multiline = true;
rtb.AcceptsTab = true;
rtb.WordWrap = false;
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
tp.Controls.Add(rtb);
tabControl1.TabPages.Add(tp);
string strfilename = openFileDialog1.FileName;
string[] lines = File.ReadAllLines(strfilename);
string[] pathArr = strfilename.Split('\\');
string[] fileArr = pathArr.Last().Split();
string filen = fileArr.Last().ToString();
tp.Text = filen;
int pyear = 0;
int pmon = 0;
int imon = 0;
int iyear = 0;
foreach (string line in lines)
{
string missing = "-9999";
string year = line.Substring(10, 4);
string mon = line.Substring(14, 2);
iyear = Convert.ToInt32(year);
imon = Convert.ToInt32(mon);
if (pyear == 0)
{
pyear = iyear;
pmon = imon;
rtb.AppendText(line + "\n");
}
else
{
int pt = pyear * 12 + pmon;
int t = iyear * 12 + imon;
if ((pt + 1) == t)
{
rtb.AppendText(line + "\n");
}
else
{
rtb.AppendText("Missing Months =" + (t - pt) + "\n");
}
if (line.Contains(missing))
{
rtb.AppendText("Missing Days" + "\n");
}
pyear = iyear;
pmon = imon;
}
rtb.SelectAll();
rtb.SelectionAlignment = HorizontalAlignment.Left;
rtb.SelectionFont = new Font("Consolas", 10);
}
}
}
}
My Question is, Is there a way of exporting all the lines before the missing month or day to a text file named the start date to the date before the missing month or day. E.g 1926.9.1926.10.txt. Then continuing through the file for the next section of data before the next missing month or day. So essentially ending up with multiple text documents containing Years or Months of Data with out gaps. I would also like it to automatically create a folder with the Station Number which is the first 14 characters (i.E 0005880 W) where all the text files will be created in.
Update
public void findGapsToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage tp = new TabPage();
RichTextBox rtb = new RichTextBox();
rtb.Dock = DockStyle.Fill;
rtb.Multiline = true;
rtb.AcceptsTab = true;
rtb.WordWrap = false;
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
tp.Controls.Add(rtb);
tabControl1.TabPages.Add(tp);
string strfilename = openFileDialog1.FileName;
string[] lines = File.ReadAllLines(strfilename);
string[] pathArr = strfilename.Split('\\');
string[] fileArr = pathArr.Last().Split();
string filen = fileArr.Last().ToString();
string pat = #"C:\Test\" + filen;
System.IO.Directory.CreateDirectory(pat);
int i;
tp.Text = filen;
int pyear = 0;
int pmon = 0;
int imon = 0;
int iyear = 0;
int j = 1;
foreach (string line in lines)
{
using (StreamWriter sw = new StreamWriter(#"C:\Test\" + filen+".txt"))
{
string missing = "-9999";
string year = line.Substring(10, 4);
string mon = line.Substring(14, 2);
iyear = Convert.ToInt32(year);
imon = Convert.ToInt32(mon);
string filepath = #"C:\Test\" + year + "." + mon+".txt";
if (pyear == 0)
{
File.CreateText(filepath);
pyear = iyear;
pmon = imon;
rtb.AppendText(line + "\n");
sw.WriteLine(line);
}
else
{
File.CreateText(filepath);
int pt = pyear * 12 + pmon;
int t = iyear * 12 + imon;
if ((pt + 1) == t)
{
rtb.AppendText(line + "\n");
sw.WriteLine(line);
}
else
{
string path = pat + "\\" + year + "." + mon + ".txt";
File.CreateText(path);
rtb.AppendText("Missing Months =" + (t - pt) + "\n");
}
if (line.Contains(missing))
{
string path = pat + "\\" + year + "." + mon + ".txt";
File.CreateText(path);
rtb.AppendText("Missing Days" + "\n");
}
pyear = iyear;
pmon = imon;
}
rtb.SelectAll();
rtb.SelectionAlignment = HorizontalAlignment.Left;
rtb.SelectionFont = new Font("Consolas", 10);
}
}
}
}
}

You can create files using the various methods of the System.IO.File class:
http://msdn.microsoft.com/en-us/library/system.io.file.aspx
This class includes methods for creating files as well as writing out arbitrary text lines to one.
You can create directories using the methods of the System.IO.Directory class:
http://msdn.microsoft.com/en-us/library/system.io.directory.aspx
UPDATE: Here is some pseudocode
startdate = null
foreach(line in the input file)
{
currentdate = date on this line in the input file
if(startdate == null)
{
// We are at the start of a new block of dates
startdate = currentdate
add this line to a list (in memory)
}
else if(currentdate == lastdate in the list + 1 month)
{
// This date is consecutive
add this line to a list (in memory)
}
else
{
// We have a gap in the data
write out all data in the list to file named <startdate>-<lastdate in list>
startdate = currentdate
add this line to the list (which we've just emptied)
}
}
write out the last file
This is just very rough and ready, but should indicate the kind of way you need to think to write this code. One thing to be clear on, if you want to name the file using the end date of a block of dates you can't create the file until you've found the last line in that block so you need to store the lines in memory until you find a gap in the dates, or the end of your input file.

Related

How to add spaces dropdownlist values in MVC

I have a dropdownlist in a view like this,
<style type="text/css">
.form-control {
width: 50%;
padding: 10px;
}
#Html.DropDownListFor(x => x.FileName, ((IEnumerable<SelectListItem>)ViewData["Items"]), new { size = 15, #class = "form-control" , #style = "padding: 10px;"})
#Html.ValidationMessageFor(x => x.FileName)
The values look like below,
aaa.txt (2015-01-01) (0 B)
abcdedfff.txt (2015-02-01) (17 MB)
I want to add some spaces between these items so that it would look like below,
aaa.txt (2015-01-01) (0 B)
abcdedfff.txt (2015-02-01) (17 MB)
The controller has below code,
if (Directory.Exists(path))
{
files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
.Select(Path.GetFileName).OrderBy(f => f).ToArray();
}
long fSize = 0;
var count = 0;
var fileModified = "";
string fileSize = "";
string[] sizes = { "B", "KB", "MB", "GB" };
foreach (var filename in files)
{
var fileInfo = new FileInfo(path + "\\" + filename);
fSize = fileInfo.Length;
int order = 0;
while (fSize >= 1024 && order + 1 < sizes.Length)
{
order++;
fSize = fSize / 1024;
}
fileSize = String.Format("{0:0.##} {1}", fSize, sizes[order]);
fileModified = fileInfo.LastWriteTime.ToString("yyyy-MM-dd hh:mm:ss tt");
SelectListItem file = new SelectListItem() { Value = count.ToString(), Text = filename + " ( " + fileModified + " )" + " ( " + fileSize + " )" };
fileItems.Add(file);
count++;
}
How do I do that?
That's a lot of code to go into one of your Controller's methods. I would create different classes, maybe as services, that do all of that work for you.
I think the only way to do this is to create a service that creates the space for you.
You can modify the code you already have to create the space you want, or you can create another class that does it for you.
public class SelectListService
{
public IEnumerable<SelectListItem> GetData()
{
// Get your data
// create your spacing
// return your formatted SelectList
}
}
Then in your controller you just have to...
var selectListService = new SelectListService();
fileItems = selectListService.GetData();
Then of course pass it into your View however you're already doing it.
I think you have to get the highest length of your filename in your collection and than use right padding and space as padding character to your filename.
Try :
if (Directory.Exists(path))
{
files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
.Select(Path.GetFileName).OrderBy(f => f).ToArray();
}
long fSize = 0;
var count = 0;
var fileModified = "";
string fileSize = "";
string[] sizes = { "B", "KB", "MB", "GB" };
int longest_file_length = files.Max(a => a.Length);
foreach (var filename in files)
{
var fileInfo = new FileInfo(path + "\\" + filename);
fSize = fileInfo.Length;
int order = 0;
while (fSize >= 1024 && order + 1 < sizes.Length)
{
order++;
fSize = fSize / 1024;
}
fileSize = String.Format("{0:0.##} {1}", fSize, sizes[order]);
fileModified = fileInfo.LastWriteTime.ToString("yyyy-MM-dd hh:mm:ss tt");
string padded_file_name = filename.PadRight(longest_file_length, ' ');
SelectListItem file = new SelectListItem() { Value = count.ToString(), Text = padded_file_name + " ( " + fileModified + " )" + " ( " + fileSize + " )" };
fileItems.Add(file);
count++;
}
Also, you have to use monospace font so that the width of each character is same.
{ font-family:"Courier New", Courier, monospace; }
You could try alignment with String.Format like below:
SelectListItem file = new SelectListItem() { Value = count.ToString(), Text = String.Format("{0,-25} ({1}) ({2})", filename, fileModified, fileSize) };
You can see example at this link and read more about string formatting here.
Here is how you should modify your method if you want deservable output:
var maxFileNameLength = files.Max(x => x.Length);
foreach (var filename in files)
{
var fileInfo = new FileInfo(path + "\\" + filename);
fSize = fileInfo.Length;
int order = 0;
while (fSize >= 1024 && order + 1 < sizes.Length)
{
order++;
fSize = fSize / 1024;
}
fileSize = String.Format("{0:0.##} {1}", fSize, sizes[order]);
fileModified = fileInfo.LastWriteTime.ToString("yyyy-MM-dd hh:mm:ss tt");
fileNameWithSpaces = filename + string.Concat(Enumerable.Repeat(" ", maxFileNameLength + 1 - filename.Length));
SelectListItem file = new SelectListItem() { Value = count.ToString(), Text = fileNameWithSpaces + " ( " + fileModified + " )" + " ( " + fileSize + " )" };
fileItems.Add(file);
count++;
}
Ok, i check it. If you want to show your spaces in drop down you should use   rather that space. I updated the code
how about something like this
$("#selectId > option").each(function() {
var thistext=$(this).text();
var brindex=thistext.indexOf('(');
var firststring=thistext.substr(0,brindex)+" ";
var laststring=thistext.substr(brindex,thistext.length);
$(this).text(firsstring+laststring);
});
not tested but it should work

Detecting repetition of part of received data

Based on the code shown.. Am I writing the right coding if i want to compare the data that were being stream in? Basically starting from the part
while(serialPort1.IsOpen)
For instance first string of data received was T 12 29.5 then next string was T 12 29.5 followed by T 20 24.5 and on so.. basically unpredictable what going to be received next.
I want to program to be able to detect/count the number of appearance for the middle value..like...
====================
[number] | [Repeated times]
12 | 2
=================== but when another different number received,
[number] | [Repeated]
20 | 1
=================== the counter for the number will be overwrite and reset whenever a different number was received.
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string time = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.ff");
RxString = serialPort1.ReadLine();
string[] split = RxString.Split('.');
string dp = split[1];
Char c = dp[0];
split[1] = c.ToString();
RxString = split[0] + "." + split[1];
while (serialPort1.IsOpen)
{
string[] number = RxString.Split(' ');
string unit = number[1];
int count = 1;
for(int i = 1; i < unit.Count(); i++)
{
if(unit[i-1] == unit[i])
count++;
else
count = 1;
if(count == 4)
{
//execute some parameters
}
}
}
this.Invoke(new EventHandler(DisplayText));
StreamWriter MyStreamWriter = new StreamWriter(#"C:\Users\acer\Documents\Data3.txt", true);
MyStreamWriter.Write(time + " " + RxString + "\r\n");
MyStreamWriter.Flush();
MyStreamWriter.Close();
}
EDIT V2
Why wont the prog record data which only has count of 1?
string[] number = RxString.Split(' '); //split RxString by ' '
string unit = number[1]; //unit = unit no.
int count = 1;
for (int i = 1; i < unit.Count(); i++)
{
if (unit[i - 1] == unit[i])
count++;
else
{
count = 1;
StreamWriter MyStreamWriter = new StreamWriter(#"C:\Users\acer\Documents\Data3.txt", true); //True tell SW to append to file instead of overwriting
MyStreamWriter.Write(time + " " + RxString + "\r\n"); //Write time + string
MyStreamWriter.Flush();
MyStreamWriter.Close();
}
You should use a dictionary to store each element and its own count :
var dict = new Dictionary<string, int?>();
while (serialPort1.IsOpen)
{
string[] number = RxString.Split(' ');
string unit = number[1];
if (dict.ContainsKey(unit))
{
if (dict[unit].HasValue)
{
dict[unit]++;
if (dict[unit] == 4)
{
// execute some parameters
dict[unit] = null;
}
}
}
else
{
dict.Add(unit, 1);
}
}
I'd create a special struct for that:
struct DataInfo
{
public string Number { get; set; }
public int Counter { get; set; }
... Other data you require to work with
}
And use either List<DataInfo> or Dictionary<string, DataInfo> to store values;

Parse unique string from complicated serial data C#

I need to parse this string from serial:-
!00037,00055#
00037 as one string, 00055 as another string
However this string is came out when the robot's tire is rotated and some other string may also display before and after the string that I need to parse. For example this is the some of the transmission received:-
11,00085#R-STOPR-STOP!00011,00095#!00001,00015#R-STOP!00001,00085#!00003,00075#!00006,00015#R-STOP!00009,00025#!00011,00035#!00011,00085#R-STOPR-STOP!00011,00095#!00001,00015#R-STOP!00001,00085#!00003,00075#!00006,00015#R-STOP!00009,00025#!00011,00035#R-STOP!00001,00085#!00003,00075#!00006,00015#R-STOP!00009,00025#!00011,00035#R-STOP!00037,00055#!00023,00075#R-STOPR-STOP!00022,00065#!00011,00085#R-STOPR-STOP!00011,00095#!00001,00015#R-STOP!00001,00085#!00003,00075#!00006,00015#R-STOP!00009,00025#!00011,00035#R-STOP!00037,00055#!00023,00075#R-STOPR-STOP!00022,00065#!00011,00085#R-STOPR-STOP!00011,00095#!00001,00015#
So far I'm stuck at what to do next after SerialPort.ReadExisting()
Here is some code to retrieve the serial data:-
private void serialCom_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
InputData = serialCom.ReadExisting();
if (InputData != String.Empty)
{
this.BeginInvoke(new SetTextCallback(IncomingData), new object[] { InputData });
}
}
catch
{
MessageBox.Show("Error");
}
}
and display incoming serial data inside textbox
private void IncomingData(string data)
{
tb_incomingData.AppendText(data);
tb_incomingData.ScrollToCaret();
}
This code is using .NET Framework 4.0 and Windows Form.
Finally solve it using indexof and substring.
private void IncomingData(string data)
{
//Show received data in textbox
tb_incomingData.AppendText(data);
tb_incomingData.ScrollToCaret();
//Append data inside longdata (string)
longData = longData + data;
if (longData.Contains('#') && longData.Contains(',') && longData.Contains('!'))
{
try
{
indexSeru = longData.IndexOf('!'); //retrieve index number of the symbol !
indexComma = longData.IndexOf(','); //retrieve index number of the symbol ,
indexAlias = longData.IndexOf('#'); //retrieve index number of the symbol ,
rotation = longData.Substring(indexSeru + 1, 5); //first string is taken after symbol ! and 5 next char
subRotation = longData.Substring(indexComma + 1, 5); //second string is taken after symbol ! and 5 next char
//tss_distance.Text = rotation + "," + subRotation;
longData = null; //clear longdata string
}
catch
{
indexSeru = 0;
indexComma = 0;
indexAlias = 0;
}
}
}
You can determine your pattern to transform this string into a array using SPLIT function.
This code, send "!00037,00055#" returns two itens: 00037 and 00055.
static void Main(string[] args)
{
string k = "!00037,00055#";
var array = k.ToString().Split(',');
Console.WriteLine("Dirty Itens");
for (var x = 0; x <= array.Length - 1; x++)
{
var linha = "Item " + x.ToString() + " = " + array[x];
Console.WriteLine(linha);
}
Console.WriteLine("Cleaned Itens");
for (var x = 0; x <= array.Length - 1; x++)
{
var linha = "Item " + x.ToString() + " = " + CleanString(array[x]);
Console.WriteLine(linha);
}
Console.ReadLine();
}
public static string CleanString(string inputString)
{
string resultString = "";
Regex regexObj = new Regex(#"[^\d]");
resultString = regexObj.Replace(inputString, "");
return resultString;
}

How to get text from slide in C# using Aspose

i am getting all shapes in slides of ppt file now i want to get text from those shapes how can i do this
here is my method where i am getting shapes of all slides in ppt file
public void Main(string[] args)
{
// The path to the documents directory.
string dataDir = Path.GetFullPath(#"C:\Users\Vipin\Desktop\");
//Load the desired the presentation
Presentation pres = new Presentation(dataDir + "Android.ppt");
using (Presentation prestg = new Presentation(dataDir + "Android.ppt"))
{
//Accessing a slide using its slide index
int slideCount = prestg.Slides.Count();
for (int i = 0; i <= slideCount - 1; i++)
{
ISlide slide = pres.Slides[i];
foreach (IShape shap in slide.Shapes)
{
int slideCountNumber = i + 1;
float shapeHeight = shap.Frame.Height;
float shapeWidth = shap.Frame.Width;
Debug.Write("slide Number: " + slideCountNumber + " shape width = " + shapeWidth + " shapeHeight = " + shapeHeight);
}
}
}
}
now ho can i get the text from it
aspose will give u truncated text if u don't have the license of it. so it will be better for you if you will use Microsoft.Office.Interop.PowerPoint
use as below
public void ReadSlide(){
string filePath= #"C:\Users\UserName\Slide.pptx";
Microsoft.Office.Interop.PowerPoint.Application PowerPoint_App = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentations multi_presentations = PowerPoint_App.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation presentation = multi_presentations.Open(filePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
string presentation_textforParent = "";
foreach (var item in presentation.Slides[1].Shapes)
{
var shape = (Microsoft.Office.Interop.PowerPoint.Shape)item;
if (shape.HasTextFrame == MsoTriState.msoTrue)
{
if (shape.TextFrame.HasText == MsoTriState.msoTrue)
{
var textRange = shape.TextFrame.TextRange;
var text = textRange.Text;
presentation_textforParent += text + " ";
}
}
}
}
You may want to extract text not from all shapes, but from text frames instead. In order to do this use the GetAllTextFrames static method exposed by the PresentationScanner class
using (Presentation prestg = new Presentation(dataDir + "Android.ppt"))
{
//Get an Array of ITextFrame objects from all slides in the PPTX
ITextFrame[] textFramesPPTX = Aspose.Slides.Util.SlideUtil.GetAllTextFrames(pptxPresentation, true);
//Loop through the Array of TextFrames
for (int i = 0; i < textFramesPPTX.Length; i++)
//Loop through paragraphs in current ITextFrame
foreach (IParagraph para in textFramesPPTX[i].Paragraphs)
//Loop through portions in the current IParagraph
foreach (IPortion port in para.Portions)
{
//Display text in the current portion
Console.WriteLine(port.Text);
//Display font height of the text
Console.WriteLine(port.PortionFormat.FontHeight);
//Display font name of the text
if (port.PortionFormat.LatinFont != null)
Console.WriteLine(port.PortionFormat.LatinFont.FontName);
}
See documentation

Merge 2 Files and throw an error if an Invalid File selected

Hi all i am having a list box on my Form which will display all the .txt files from the directory C:. This list box selection mode is set to MultiExtended.
My condition to check whether the file is valid or not will be checked using the condition as each and every line content of the selected file should be *94*. If this one satisifes then only it is said to be a valid file. I have written a code for this too but as i am checking in the loop and at a time i can only read one file content this was working fine. But i have to check initially all the selected files matches the condition or not if ok then i have to do the remaining code if not i would like to display error
My code on Button Click
private void btnMerge_Click(object sender, EventArgs e)
{
if (lstACH.SelectedIndices.Count == 1)
{
MessageBox.Show("Select 2 Files To Merge");
}
else
{
for (i = 0; i < lstACH.SelectedItems.Count; i++)
{
strFile = lstACH.SelectedItems[i].ToString();
lines = File.ReadAllLines(strFile);
if (LinesHaveCorrectLength(lines, 94)) // Here i am checking but at a tym i am checking one file only i have to check for all and if ok then the remaining code has to be executed
{
if (i == 0)
{
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = #"C:\";
saveFileDialog1.DefaultExt = "txt";
saveFileDialog1.Filter = "(*.txt)|*.txt";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.ValidateNames = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Append.FileName = saveFileDialog1.FileName;
if (Append.FileName.Contains(" \\/:*?<>|"))
{
MessageBox.Show("File name should not contain \\/:*?<>|", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
Append.FileName = saveFileDialog1.FileName;
myStream.Close();
}
}
}
}
using (StreamWriter sw = new StreamWriter(Append.FileName, true))
{
using (StreamReader srBatch = new StreamReader(strFile))
{
while (srBatch.Peek() >= 0)
{
strReadLine = srBatch.ReadLine();
if (strReadLine.StartsWith("1"))
{
if (i == 0)
{
strFileHeader = strReadLine;
sw.WriteLine(strFileHeader);
}
}
if (strReadLine.StartsWith("5"))
{
strBtchHeader = strReadLine;
if (i == 0)
{
Btchno = Convert.ToInt32(strReadLine.Substring(87, 7));
BatchCnt = Convert.ToInt16(Btchno);
}
if (i > 0)
{
BatchCnt++;
strBtchHeader = strBtchHeader.Substring(0, 87) + Convert.ToString(BatchCnt.ToString().PadLeft(7, (char)48));
}
sw.WriteLine(strBtchHeader);
}
if (strReadLine.StartsWith("6"))
{
strEntryDetail = strReadLine;
if (i == 0)
{
strTraceNo = strEntryDetail.Substring(87, 7);
EntryCount = Convert.ToInt16(strTraceNo);
}
if (i > 0)
{
EntryCount++;
strEntryDetail = strEntryDetail.Substring(0, 87) + EntryCount.ToString().PadLeft(7, (char)48);
}
sw.WriteLine(strEntryDetail);
}
if (strReadLine.StartsWith("8"))
{
strBtchCntrl = strReadLine;
if (i > 0)
{
//btchEntry++;
strBtchCntrl = strBtchCntrl.Substring(0, 87) + BatchCnt.ToString().PadLeft(7, (char)48);
}
sw.WriteLine(strBtchCntrl);
}
if (strReadLine.StartsWith("9"))
{
strFileCntrl = strReadLine;
strBtchCnt = strReadLine.Substring(1, 6);
strEntrycnt = strReadLine.Substring(13, 8);
strEntryHash = strReadLine.Substring(21, 10);
strDebitAmnt = strReadLine.Substring(31, 12);
strCreditAmnt = strReadLine.Substring(43, 12);
BtchCnt += Convert.ToDouble(strBtchCnt);
Entrycnt += Convert.ToDouble(strEntrycnt);
EntryHash += Convert.ToDouble(strEntryHash);
DebitAmnt += Convert.ToDouble(strDebitAmnt);
CreditAmnt += Convert.ToDouble(strCreditAmnt);
if (i == lstACH.SelectedItems.Count - 1)
{
strFileCntrl = strFileCntrl.Substring(0, 1) + BtchCnt.ToString().PadLeft(6, (char)48) + strFileCntrl.Substring(7, (strFileCntrl.Length - 7));
strFileCntrl = strFileCntrl.Substring(0, 13) + Entrycnt.ToString().PadLeft(8, (char)48) + strFileCntrl.Substring(21, (strFileCntrl.Length - 21));
strFileCntrl = strFileCntrl.Substring(0, 21) + EntryHash.ToString().PadLeft(10, (char)48) + strFileCntrl.Substring(31, (strFileCntrl.Length - 31));
strFileCntrl = strFileCntrl.Substring(0, 31) + DebitAmnt.ToString().PadLeft(12, (char)48) + strFileCntrl.Substring(43, (strFileCntrl.Length - 43));
strFileCntrl = strFileCntrl.Substring(0, 43) + CreditAmnt.ToString().PadLeft(12, (char)48) + strFileCntrl.Substring(55, (strFileCntrl.Length - 55));
sw.WriteLine(strFileCntrl);
}
}
}
}
}
if (i == lstACH.SelectedItems.Count - 1)
{
MessageBox.Show("File Has Been Merged Successfully");
this.Close();
}
}
else
{
MessageBox.Show("One of the Selected File is not a Valid ACH File");
break;
}
}
}
}
Checking for Each and every line Length
private static bool LinesHaveCorrectLength(string[] lines, int expectedLineLength)
{
foreach (string item in lines)
{
if (item.Length != expectedLineLength)
{
return false;
}
}
return true;
}
Just check them all first - and if all good, THEN begin merge.
if (lstACH.SelectedIndices.Count != 2)
{
MessageBox.Show("Select 2 Files To Merge");
return;
}
foreach (String fileName in lstACH.SelectedItems)
{
if( LinesHaveCorrectLength( File.ReadAllLines(fileName), 94 ) == false )
{
MessageBox.Show("File: " + fileName + " has an incorrect line length");
return;
}
}
// Now process them all again to merge:
foreach(String fileName in lstACH.SelectedItems)
{
// ... do merge logic
}
Ok according to your comment it looks like First you want both files to be validated. if that is the case then:
(There could be many ways , this is one of them)
First define a Function to do the checking for all files:
public bool AreFilesValid(ListBox.SelectedObjectCollection filenames)
{
int count = filenames.Count;
bool valid = false;
for(int i=0;i<count;i++)
{
string strFile = filenames[i].ToString();
string[] lines = File.ReadAllLines(strFile);
if(LinesHaveCorrectLength(lines, 94)) { valid=true; }
else { valid = false; }
}
return valid;
}
Then call it in your if condition, i.e just change following lines:
...
strFile = lstACH.SelectedItems[i].ToString();
lines = File.ReadAllLines(strFile);
if (LinesHaveCorrectLength(lines, 94))
{
...
To only this:
...
if (AreFilesValid(lstACH.SelectedItems))
{
...
You have already got your else statement down the code to catch when this if condition fails.

Categories