splitting my text file based on textbox value. [Resolved]

Posted by Anishabathija under ASP.NET on 12/11/2012 | Points: 10 | Views : 1609 | Status : [Member] | Replies : 11
hey ,

I have a fileuploader to upload my file and a textbox to enter a value.

Now I want to split my file based on rows. Like if the textbox value is 4 , then my text file must split at every 4 rows and create new text files.

Please help me with a code. its urgent !!




Responses

Posted by: Pavanandey on: 12/11/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Btn1" Text="click" runat="server" onclick="Btn1_Click" />

protected void Btn1_Click(object sender, EventArgs e)
{
StreamReader Sr = new StreamReader(FileUpload1.PostedFile.FileName);
string Line = string.Empty; ;
int CountLine = 0;
int countfile = 1;
string[] lines = new string[100];
while ((Line = Sr.ReadLine()) != null)
{
for (CountLine = 0; CountLine < 5; CountLine++)
{
lines[CountLine] = Line;
Line = Sr.ReadLine();
}
string newpath = "C:\\Users\\pavan\\Desktop\\Hi\\" + countfile + ".txt";
System.IO.File.WriteAllLines(@newpath, lines);
countfile++;
}
Sr.Close();
}

Mark answer if this helps you

Thanks
Pavan Kumar
Mark Answer if this fits the need

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

Posted by: Pavanandey on: 12/11/2012 [Member] Bronze | Points: 25

Up
0
Down
StreamReader Sr = new StreamReader("C:\\Users\\pavan\\Desktop\\1.txt");
string Line = string.Empty; ;
int CountLine = 0;
int countfile = 1;
string[] lines = new string[100];
while ((Line = Sr.ReadLine()) != null)
{
for (CountLine = 0; CountLine < 5; CountLine++)
{
lines[CountLine] = Line;
Line = Sr.ReadLine();
}
string newpath = "C:\\Users\\pavan\\Desktop\\Hi\\" + countfile + ".txt";
System.IO.File.WriteAllLines(@newpath, lines);
countfile++;
}
Sr.Close();

here i have specfied 5, you can make it as you textbox value

Mark the answer if this helps

Thanks
Pavan Kumar
Mark Answer if this fits the need

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

Posted by: Anishabathija on: 12/11/2012 [Member] Starter | Points: 25

Up
0
Down
Hey please tell me how to use the file upload control , for the above code

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

Posted by: Anishabathija on: 12/11/2012 [Member] Starter | Points: 25

Up
0
Down
hey thanks one last question.
How do I pass my textbox value.

im using
int value= int32.parse(textbox1.tostring());

But its throwing an error in the for loop saying int and string values cant be used.
Please help

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

Posted by: Pavanandey on: 12/11/2012 [Member] Bronze | Points: 25

Up
0
Down
can you post me your code

Thanks
Pavan Kumar
Mark Answer if this fits the need

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

Posted by: Pavanandey on: 12/11/2012 [Member] Bronze | Points: 25

Up
0
Down
for (CountLine = 0; CountLine < Convert.ToInt32(txtinput.Text); CountLine++)

make sure your textbox has some numeric value, hope you are failing here.

Thanks
Pavan Kumar
Mark Answer if this fits the need

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

Posted by: Anishabathija on: 12/11/2012 [Member] Starter | Points: 25

Up
0
Down
my code is as follows right now :

if (FileUploadControl.HasFile)
{
try
{
string filename = Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
}
catch (Exception ex)
{
throw;
}
StreamReader Sr = new StreamReader();
string Line = string.Empty; ;
int CountLine = 0;
int countfile = 1;
string[] lines = new string[100];
while ((Line = Sr.ReadLine()) != null)
{
for (CountLine = 0; CountLine < 5; CountLine++)
{
lines[CountLine] = Line;
Line = Sr.ReadLine();
}
string newpath = "C:\\Users\\Anisha\\Desktop\\Split" + countfile + ".txt";
System.IO.File.WriteAllLines(@newpath, lines);
countfile++;
}
Sr.Close();



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

Posted by: Anishabathija on: 12/11/2012 [Member] Starter | Points: 25

Up
0
Down
when i used the aboce code for file upload in stream reader line . It said path could not be found.

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

Posted by: Anishabathija on: 12/11/2012 [Member] Starter | Points: 25

Up
0
Down
and suppose I want to add the first row and last row of my file in all the split files how do i go about it?

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

Posted by: Pavanandey on: 12/11/2012 [Member] Bronze | Points: 25

Up
0
Down
first of all you havent assigned your file to the stream reader and you have started reading the file.

you can modify the above login so you can add first and last lines. try making it

Thanks
Pavan Kumar
Mark Answer if this fits the need

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

Posted by: Anishabathija on: 12/11/2012 [Member] Starter | Points: 25

Up
0
Down
StreamReader Sr = new StreamReader(FileUpload1.PostedFile.FileName);


I passed that.. but it said file path could not be found.

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

Login to post response