Split the notepad based on the line

Posted by Agopi.Net under C# on 7/13/2012 | Points: 10 | Views : 1694 | Status : [Member] | Replies : 4
I want to split the notepad according to line count. I have 1000 records, i need to split this each 100 records or each 50 records.....

According to this, file need to split and save in C # code.

Could anyone help me.

By
Gopi A




Responses

Posted by: CGN007 on: 7/13/2012 [Member] Silver | Points: 25

Up
0
Down
Let me know,What you have done so far...?

Agopi.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.Net on: 7/14/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Below are code... Please rectify my problem....

private void button1_Click(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines("D:\\blogspot.txt");

int partitionSize = 2;

int partitionNumber = (lines.Length - 1) / partitionSize;
int partitionRemainder = (lines.Length - 1) % partitionSize;
string[] partitionLines = new string[partitionSize + 1];
partitionLines[0] = lines[0]; // copy heading

int part;

for(part = 1; part <= partitionNumber; part++)
{
int startIndex = (part - 1) * partitionSize + 1;

Array.Copy(lines, startIndex, partitionLines, 1, partitionSize);

string fileName = "gopi" + part.ToString() + ".txt";

using (StreamReader sr = File.OpenText(fileName ) )

{

string s = "";

while ((s = sr.ReadLine()) != null)
{

File.WriteAllLines(s.ToString ());
}
}

}

if (partitionRemainder > 0)
{
int startIndex = (part - 1) * partitionSize + 1;
partitionLines = new string[partitionRemainder + 1];
partitionLines[0] = lines[0];
Array.Copy(lines, startIndex, partitionLines, 1, partitionRemainder);
string fileName = "gopi" + part.ToString() + ".txt";

//File.WriteAllLines(fileName, partitionLines);

FileStream fs = null;
if (!File.Exists(fileName ))
{
using (fs = File.Create(fileName.ToString() ))
{
File.WriteAllLines(fileName, partitionLines);
}
}

}
}


Agopi.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.Net on: 7/15/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Team,

I need this in windows application.... Not in Console application....

Agopi.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.Net on: 7/15/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

I have completed this...

string[] lines = File.ReadAllLines("C:\\Users\\VG\\Desktop\\nexgen.txt");

int partitionSize = 2;

int partitionNumber = (lines.Length - 1) / partitionSize;
int partitionRemainder = (lines.Length - 1) % partitionSize;
string[] partitionLines = new string[partitionSize + 1];
partitionLines[0] = lines[0]; // copy heading

int part;

for(part = 1; part <= partitionNumber; part++)
{
int startIndex = (part - 1) * partitionSize + 1;

Array.Copy(lines, startIndex, partitionLines, 1, partitionSize);

string fileName = "D:\\gopi" + part.ToString() + ".txt";

File.WriteAllLines(fileName, partitionLines);

}

if (partitionRemainder > 0)

{

int startIndex = (part - 1) * partitionSize + 1;

partitionLines = new string[partitionRemainder + 1];

partitionLines[0] = lines[0];

Array.Copy(lines, startIndex, partitionLines, 1, partitionRemainder);

string fileName = "D:\\gopi" + part.ToString() + ".txt";

File.WriteAllLines(fileName, partitionLines );

}
}


Agopi.Net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response