Hi,
I have the 1000 records in noteapd. I want to split it by 100 records (based on the line count). In each split file should have the header along with data. Please help me.
Regards,
Gopi A
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jul 15, 2012, 9:23 AM
You could generalize it by reading the path of the file to split up from one textbox and the partition size from another textbox.
Gopi AsaiPosted Jul 15, 2012, 4:20 AM
I have changed this in windows application.
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 );
}
Gopi AsaiPosted Jul 15, 2012, 12:45 AM
Your initial code is working fine. I tried this in windows application instead of console application.
I am very sorry to say this.
Thank you very much for your clarification. Could you please let me know this for windows application.
VulpesPosted Jul 14, 2012, 12:52 PM
If you've put gopi.txt in the root directory of your D: drive, then try writing the partition files to the same drive so that you know exactly where they should be. So this line:
string fileName = "gopi" + part.ToString() + ".txt";
should be changed to this:
string fileName = "D:\\gopi" + part.ToString() + ".txt";
Gopi AsaiPosted Jul 13, 2012, 1:43 PM
VulpesPosted Jul 13, 2012, 5:13 AM
In the same directory, you should have 10 more files containing the header and 100 lines each.
These 'partition' files will be named gopi1.txt, gopi2.txt etc.
When you say the file did not save, do you mean that the partition files haven't been created at all or that they have been created but don't contain what they should?
Gopi AsaiPosted Jul 13, 2012, 2:05 AM
The file did not save, Even I changed the file path. Below is the path.
string[] lines = File.ReadAllLines("D:\\gopi.txt");
Please help me.
VulpesPosted Jul 12, 2012, 6:42 PM
Gopi AsaiPosted Jul 12, 2012, 2:44 PM
While i put the breakpoints to all line i could saw the file has been split. But i dont know where it was saved. Could you please let me know the split-up file location.
VulpesPosted Jul 12, 2012, 5:19 AM