How to get text from slide in C# using Aspose - c#

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

Related

Reading shapes and textboxes in EPPlus 4.5.3

I am trying to read an excel file using EPPlus version 4.5.3, which I am able to do using the code below:
FileInfo existingFile = new FileInfo(FilePath);
using (ExcelPackage package = new ExcelPackage(existingFile))
{
//get the first worksheet in the workbook
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int colCount = worksheet.Dimension.End.Column; //get Column Count
int rowCount = worksheet.Dimension.End.Row; //get row count
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= colCount; col++)
{
Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value?.ToString().Trim());
}
}
}
Now, the place I am getting stuck at is with shapes. So the excel file that I need to read have shapes in it, these shapes have text inside it that I am trying to read. I have tried searching on the internet for this problem but I cant seem to find anything on it.
How can I read this data? The code I have tried thus far:
foreach (var drawing in sheet.Drawings)
{
var type = drawing.GetType();
var data = drawing.ToString();
Console.WriteLine("Drawing Type:" + type + " Data: " + data);
}
I was just having this issue today and figured it out. You have to iterate the worksheet.Drawings collection first to determine if any drawings are "shapes". From what I know of Excel VBA, you can not put text on an image/picture it HAS to be a shape. Someone can correct me if I am wrong.
using (ExcelPackage excelPackage = new ExcelPackage(stream))
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
foreach (ExcelDrawing dw in worksheet.Drawings)
{
if (dw.GetType().ToString() == "OfficeOpenXml.Drawing.ExcelShape")
{
ExcelShape shape = (ExcelShape)dw;
if (shape.RichText != null && shape.RichText.Count > 0)
{
foreach (ExcelParagraph item in shape.RichText)
{
Console.WriteLine("{0} - Rich Text Line: {1}", dw.Name, item.Text);
}
}
}
else
{ Console.WriteLine("{0} is not a shape, its a {1}", dw.Name, dw.GetType().ToString()); }
}
}
From there it should be rather easy jump to modify the text in the picture:
item.RichText[1].Text = "Updated Text";
Output:
Picture 1 is not a shape, its a OfficeOpenXml.Drawing.ExcelPicture
TextBox 1 - Rich Text Line: Inventory List
TextBox 1 - Rich Text Line: Some Company

How to do a clickable file path in asp.net c#

I am trying to get a clickable file path using asp.net c# visual studios web form, meaning to say that it is like windows file explorer, allowing the person to navigate through the different levels of folders etc, can anyone provide any links to help get me started? [1]: https://i.stack.imgur.com/WyyLq.png
You could try to get the path string and divide it in multiple pieces. Then store them in multiple textboxes, labels, buttons or whatever you want. My form looks like this: Picture Form
Secondly, you will have to update those (in my case) textboxes to save the path. See my code and decide what you want to use, and if you have to modify it.
private void changePath()
{
String path = webBrowser1.Url.AbsolutePath;
if (path.Contains(#"/")) { path = path.Replace(#"/", #"\"); }
string[] directories = path.Split(Path.DirectorySeparatorChar);
int count = directories.Count();
if (count <= 6)
{
textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox6.Text = "";
for (int i = 0; i < count; i++)
{
String txt = "textBox" + (i + 1);
TextBox tbx = this.Controls.Find(txt, true).FirstOrDefault() as TextBox;
tbx.Text = directories[i];
}
}
else
{
int p = count / 6;
int z = count - (p * 6);
for (int i = 0; i < count; i++)
{
int g = i - 1;
String txt = "textBox" + (i + 1);
TextBox tbx = this.Controls.Find(txt, true).FirstOrDefault() as TextBox;
tbx.Text = directories[z];
z++;
if (i == 5)
{
break;
}
}
}
}
Second step is to make click functions on the (in my case) textboxes. Here is one sample of how you can do this. See for yourself what you can do.
private void textBox5_Click(object sender, EventArgs e)
{
if(!textBox5.Text.Equals(String.Empty))
{
String p = webBrowser1.Url.AbsolutePath;
if(!textBox6.Text.Equals(String.Empty))
{
webBrowser1.Url = new Uri(p.Replace(#"/" + textBox6.Text, ""));
}
}
}
This code will remove the last piece, leaving you with a new path. Example:
Before: C:\Users\USERNAME\Desktop\C#
After:
After: C:\Users\USERNAME\Desktop
Again, you have to look what works for you. There are multiple ways to resolve your issue.
Good Luck!
Twan.

C# Read string from CSV file and plot line graph

Currently, I am able to read data from multiple CSV file and plot line graph using windows form application. However, now I need to plot a line graph based on a CSV file's section name (3rd column of csv file).
Modified/New CSV file: (Added the Section Name column)
Values,Sector,Name
5.55,1024,red
5.37,1536,red
5.73,2048,blue
5.62,2560,.blue
5.12,3072,.yellow
...
Based on the Section Name column, my line graph need to be plotted accordingly in a Single line and different sections must be plotted with different colors, including the legends shown at the side of the graph must be shown based on the different section names.
1 csv file = 1 Series. But there are same section names in a csv file (csv file example shown above, e.g. red for the 1st 20lines). Same section names = same color. If I open 2 or more csv files = 2 Series. Each Series will have different colors due to different section names in the csv file.
I am quite new with programming, and would really appreciate someone could help me by editing from my code.
Thank you.
Updated code:
GraphDemo (Form):
List<Read> rrList = new List<Read>();
void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ff = new OpenFileDialog();
Read rr;
ff.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //"C:\\";
ff.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
ff.Multiselect = true;
ff.FilterIndex = 1;
ff.RestoreDirectory = true;
if (ff.ShowDialog() == DialogResult.OK)
{
try
{
rrList.Clear();
foreach (String file in ff.FileNames) //if ((myStream = ff.OpenFile()) != null)
{
rr = new Read(file);
rrList.Add(rr);
}
//Populate the ComboBoxes
if (rrList.Count > 0)
{
string[] header = rrList[0].header; //header of first file
xBox.DataSource = header;
yBox.DataSource = header.Clone(); //without Clone the 2 comboboxes link together!
}
if (yBox.Items.Count > 1) yBox.SelectedIndex = 1; //select second item
}
catch (Exception err)
{
//Inform the user if we can't read the file
MessageBox.Show(err.Message);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Plot.Draw(rrList, xBox, yBox, chart);
}
class Read:
public class Read
{
public int nLines { get; private set; }
public int nColumns { get; private set; }
public string[] header { get; private set; }
public float[,] data { get; private set; }
public string fileName { get; set; }
public string[] section { get; private set; }
public Read(string file)
{
string[] pieces;
fileName = Path.GetFileName(file);
string[] lines = File.ReadAllLines(file); // read all lines
if (lines == null || lines.Length < 2) return; //no data in file
header = lines[0].Split(','); //first line is header
nLines = lines.Length - 1; //first line is header
nColumns = header.Length;
//read the numerical data and section name from the file
data = new float[nLines, nColumns - 1]; // *** 1 less than nColumns as last col is sectionName
section = new string[nLines]; // ***
for (int i = 0; i < nLines; i++)
{
pieces = lines[i + 1].Split(','); // first line is header
if (pieces.Length != nColumns) { MessageBox.Show("Invalid data at line " + (i + 2) + " of file " + fileName); return; }
for (int j = 0; j < nColumns - 1; j++)
{
float.TryParse(pieces[j], out data[i, j]); //data[i, j] = float.Parse(pieces[j]);
}
section[i] = pieces[nColumns - 1]; //last item is section
}
}
}
class Plot:
public class Plot
{
//public Plot() { } //no constructor required as we use a static class to be called
public static void Draw(List<Read> rrList, ComboBox xBox, ComboBox yBox, Chart chart) //***
{
int indX = xBox.SelectedIndex;
int indY = yBox.SelectedIndex;
chart.Series.Clear(); //ensure that the chart is empty
chart.Legends.Clear();
Legend myLegend = chart.Legends.Add("myLegend");
myLegend.Title = "myTitle";
//define a set of colors to be used for sections
Color[] colors = new Color[] { Color.Black, Color.Blue, Color.Red, Color.Green, Color.Magenta, Color.DarkCyan, Color.Chocolate, Color.DarkMagenta };
//use a Dictionary to keep iColor of each section
// key=sectionName, value=iColor (color index in our colors array)
var sectionColors = new Dictionary<string, int>();
int i = 0;
int iColor = -1, maxColor = -1;
foreach (Read rr in rrList)
{
float[,] data = rr.data;
int nLines = rr.nLines;
int nColumns = rr.nColumns;
string[] header = rr.header;
chart.Series.Add("Series" + i);
chart.Series[i].ChartType = SeriesChartType.Line;
//chart.Series[i].LegendText = rr.fileName;
chart.Series[i].IsVisibleInLegend = false; //hide default item from legend
chart.ChartAreas[0].AxisX.LabelStyle.Format = "{F2}";
chart.ChartAreas[0].AxisX.Title = header[indX];
chart.ChartAreas[0].AxisY.Title = header[indY];
for (int j = 0; j < nLines; j++)
{
int k = chart.Series[i].Points.AddXY(data[j, indX], data[j, indY]);
string curSection = rr.section[j];
if (sectionColors.ContainsKey(curSection))
{
iColor = sectionColors[curSection];
}
else
{
maxColor++;
iColor = maxColor; sectionColors[curSection] = iColor;
}
chart.Series[i].Points[k].Color = colors[iColor];
}
i++; //series#
} //end foreach rr
//fill custom legends based on sections/colors
foreach (var x in sectionColors)
{
string section = x.Key;
iColor = x.Value;
myLegend.CustomItems.Add(colors[iColor], section); //new LegendItem()
}
}
}
You can separate the data by the section column and use the section names as index into the Series collection instead of using i.
Best use the section name as the Series.Name. I suggest using a data class containing the two numbers and the string and collect them in a List<Dataclass>. Then create Series for the distinct sections. Then loop over them..
Here are a few code examples:
Define a class for your data:
public class Data3
{
public int N1 { get; set;}
public double N2 { get; set;}
public string S1 { get; set;}
public Data3(double n2, int n1, string s1)
{
N1 = n1; N2 = n2; S1 = s1;
}
}
Pick your own names! Optional but always recommended: Add a nice ToString() overload!
Declare a class level varible:
List<Data3> data = new List<Data3>();
During the read collect the data there:
data.Add(new Data3(Convert.ToDouble(pieces[1]), Convert.ToInt32(pieces[0]), pieces[2]));
To plot the chart first create the Series:
var sections= data.Select(x => x.S1).Distinct<string>();
foreach (string s in sections)
chart.Series.Add(new Series(s) { ChartType = SeriesChartType.Line });
Then plot the data; the series can be indexed by their Names:
foreach (var d in data) chart.Series[d.S1].Points.AddXY(d.N1, d.N2);
I left out the nitty gritty of integrating the code into your application; if you run into issues, do show the new code by editing your question!
A few notes:
When in doubt always create a class to hold your data
When in doubt always choose classes over structures
When in doubt always choose List<T> over arrays
Always try to break your code down to small chunks with helpful names.
Example: To read all the data in a csv file create a function to do so:
public void AppendCsvToDataList(string file, List<Data3> list)
{
if (File.Exists(file))
{
var lines = File.ReadAllLines(file);
for (int l = 1; l < lines.Length; l++)
{
var pieces = lines[l].Split(',');
list.Add(new Data3(Convert.ToInt32(pieces[1]),
Convert.ToDouble(pieces[0]), pieces[2]));
}
}
}

Formatting text using word.interop in outlook 2013 add-in

i'm trying to format text that i'm inserting at the beginning of a new email when a user presses a button in the ribbon add-in
the code is simple in that it first checks if the type of text was previously inserted, removes it if it was, and then inserts a new bracket of text using Word.Range.InsertBefore(.....);
the text will be:
CLASSIFICATION: .......
CLASSIFICATION: .......
the problem that i am running into is after the insert i'm formatting the font using Range.Font,Text etc...
i need the CLASSIFICATION: ...... formatted and not the space between them
the space between the "CLASSIFICATION......" is being formatted when typing begins to the same size, color, etc.... as the "CLASSIFICATION"
the code i'm using is:
private void setClassificationText(string classificationText, Word.WdColorIndex color)
{
//current mail item document
Word.Document document = (Word.Document)mailItem.GetInspector.WordEditor;
Word.Range rng = document.Paragraphs[1].Range;
//check if a classification level was already supplied
if (checkIfClassificationExists(rng))
{
//get the first 2 Paragraphs and remove the text from them
int paraCount = 2;
for (int i = 1; i <= paraCount; i++)
{
Word.Range classificationRange = document.Paragraphs[i].Range;
removeTextFromParagraph(classificationRange);
}
rng.Collapse();
rng = document.Paragraphs[1].Range;
}
rng.InsertBefore(classificationText + CT.lineFeedStr5 + CT.classification + classificationText + CT.lineFeedStr3);
//sets the color and text
rng.Font.ColorIndex = color;
rng.Text = CT.classification + rng.Text;
rng.Font.Bold = 1;
}
private void removeTextFromParagraph(Word.Range rng)
{
int wordCount = rng.Words.Count;
rng.Delete(Count: wordCount);
}
solved the issue i was having.
i used the following:
private void setClassificationText(string classificationText, Word.WdColorIndex color)
{
//current mail item document
Word.Document document = (Word.Document)mailItem.GetInspector.WordEditor;
Word.Range rng = document.Paragraphs[1].Range;
//check if a classification level was already supplied
if (checkIfClassificationExists(rng))
{
//get the first 2 Paragraphs and remove the text from them
int paraCount = 2;
for (int i = 1; i <= paraCount; i++)
{
Word.Range classificationRange = document.Paragraphs[i].Range;
removeTextFromParagraph(classificationRange);
}
rng.Collapse();
rng = document.Paragraphs[1].Range;
}
//insert the text
rng.InsertBefore(classificationText + CT.lineFeedStr5 + CT.classification + classificationText);
//sets the color and text
rng.Font.ColorIndex = color;
rng.Text = CT.classification + rng.Text;
rng.Font.Bold = 1;
//get the beginning and ending positions of the blank space
startPosition = rng.Start + getClassificationLength(classificationText);
endPosition = (int)startPosition + CT.lineFeedStr5.Length-1;
//get the Word.Range for the blank space
Word.Range rngNormal = document.Range(ref startPosition, endPosition);
//set blank space to be normal font (not bold and black)
rngNormal.Bold = 0;
rngNormal.Font.ColorIndex = Word.WdColorIndex.wdBlack;
}
private int getClassificationLength(string classificationText)
{
int returnValue = 0;
returnValue = CT.classification.Length + classificationText.Length;
return returnValue;
}
hope this can help someone else out.

Saving Specific Lines to New Text Files

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.

Categories