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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jul 12, 2012, 1:19 AM EDT
Gopi AsaiRank #6,532 · 263 reputationPosted Jul 12, 2012, 10:44 AM EDT
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.
VulpesRank #55 · 29.2K reputationPosted Jul 12, 2012, 2:42 PM EDT
Gopi AsaiRank #6,532 · 263 reputationPosted Jul 12, 2012, 10:05 PM EDT
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.
VulpesRank #55 · 29.2K reputationPosted Jul 13, 2012, 1:13 AM EDT
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 AsaiRank #6,532 · 263 reputationPosted Jul 13, 2012, 9:43 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 14, 2012, 8:52 AM EDT
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 AsaiRank #6,532 · 263 reputationPosted Jul 14, 2012, 8:45 PM EDT
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.
Gopi AsaiRank #6,532 · 263 reputationPosted Jul 15, 2012, 12:20 AM EDT
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 );
}
VulpesRank #55 · 29.2K reputationPosted Jul 15, 2012, 5:23 AM EDT
You could generalize it by reading the path of the file to split up from one textbox and the partition size from another textbox.