473,473 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

split a textfile in to two

91 New Member
I have a texfile with hundreds of records. Each record in a line has 1250 values & values are seperated by a semi colon. IS there a way in VB.NET that i can split each line for example first 1000 values stored in a seperate textfile & the rest 250 in another text file.? and like wise each line of the original text file is split the same way.
I want to import these two new text files then into SQL server 2 tables ( table one with 1000 columns & table 2 with 250 columns).
many thanks for the help.
Nov 11 '08 #1
13 1875
Plater
7,872 Recognized Expert Expert
Well if you read them in, one line at a time.
You can then do a split and put the 1000 into one file and 250 into another file.
Or you can find the index of the 1000th semi-colon and do a substring on it, to get the two seperate pieces for the files.

You will have to be carefull as that much data will eat up memory.
Nov 11 '08 #2
kashif73
91 New Member
Thanks Plater for the reply. How do I find index of the 1000th semi-colon and do a substring on it??? Can you please provide an example code. I am really new to vb.net. your help with some code will really get me started here. thanks.
Nov 11 '08 #3
Curtis Rutland
3,256 Recognized Expert Specialist
Well, if you use Split rather than Substring, you will get an array of strings, each being a separate value. So then you could just copy elements 0-999 to one file, and elements 1000 to array.length to another.

But do make sure to do this one line at a time. Use System.IO.StreamReader for the reading. Also, use StreamWriter for the writing.
Nov 11 '08 #4
kashif73
91 New Member
Thanks InserAlias. As per your suggestion please see my code below:

Expand|Select|Wrap|Line Numbers
  1. Dim FILE_NAME As String = "C:\IKN Individual\Ind-" & IDN.Text & ".ikn"
  2.         If System.IO.File.Exists(FILE_NAME) = True Then
  3.             Dim lines() As String = File.ReadAllLines(FILE_NAME)
  4.  
  5.             Dim fs As New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
  6.             Dim sr As New StreamReader(fs)
  7.  
  8.  
  9.             Dim LineIn As String
  10.             While sr.Peek <> -1
  11.                 LineIn = sr.ReadLine()
  12.  
  13.                 'Dim Contents As String = sr.ReadToEnd()
  14.                 Dim textdelimiter As String
  15.                 textdelimiter = ";"
  16.                 Dim splitout = Split(LineIn, textdelimiter)
  17.  
  18.                 Dim splitText() As String = LineIn.Split(";")
  19.  
  20.                 Dim oWrite As System.IO.StreamWriter
  21.                 'Dim oFile As System.IO.File
  22.                 oWrite = IO.File.CreateText("C:\IKN Individual\sample.txt")
  23.                 Dim J As Integer
  24.  
  25.                 For J = 0 To splitText(1000)
  26.                     oWrite.WriteLine(splitText(1000))
  27.                 Next
  28.                 oWrite.Close()
  29.  
  30.             End While 
I am confused on my FOR NEXT loop, For J = 0 To splitText(1000). How do I write so that first 1000 go to sample.txt & next 250 goes to sample 2.txt. I am attaching my sample txt file from which i get records & split.
Many thanks again foryour help.
Attached Files
File Type: txt sample.txt (5.3 KB, 297 views)
Nov 12 '08 #5
kashif73
91 New Member
Sorry this is the code i have so far. Also this is reading only 1 line of my text file only....although the split seems to work for the first 1000 & next 220. But I would like you to see & comment on it. please. thanks.
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2.  
  3.  
  4.         Dim FILE_NAME As String = "C:\IKN Individual\Ind-" & IDN.Text & ".ikn"
  5.         If System.IO.File.Exists(FILE_NAME) = True Then
  6.  
  7.  
  8.             Dim fs As New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
  9.             Dim sr As New StreamReader(fs)
  10.  
  11.             Dim LineIn As String
  12.  
  13.                         While sr.Peek <> -1
  14.                 LineIn = sr.ReadLine()
  15.                 Dim textdelimiter As String
  16.                 textdelimiter = ";"
  17.                 Dim splitText() As String = LineIn.Split(";")
  18.  
  19.                 Dim oWrite As System.IO.StreamWriter
  20.                 oWrite = IO.File.CreateText("C:\IKN Individual\sample.txt")
  21.                 Dim J As Integer
  22.  
  23.                 For J = 0 To splitText.Length - 221
  24.                     oWrite.Write(splitText(J) & ";")
  25.                 Next
  26.                 oWrite.WriteLine()
  27.                 oWrite.Close()
  28.  
  29.                 oWrite = IO.File.CreateText("C:\IKN Individual\sample2.txt")
  30.                 Dim G As Integer
  31.  
  32.                 For G = splitText.Length - 222 To splitText.Length - 1
  33.                     oWrite.Write(splitText(G) & ";")
  34.                 Next
  35.                 oWrite.WriteLine()
  36.                 oWrite.Close()
  37.  
  38.             End While
  39.  
  40.             'myReader.Close()
  41.             fs.Close()
  42.             sr.Close()
  43.         Else
  44.  
  45.             MsgBox("The TEXTFILE does Not Exist.")
  46.  
  47.         End If
  48.     End Sub 
Nov 12 '08 #6
Plater
7,872 Recognized Expert Expert
Does sr.Peek work like that? I would have gone with while NOT sr.EndOfStream or whatever.
I would also move:
Dim textdelimiter As String
textdelimiter = ";"

out of loops, as you don't really need to be creating and destroying this string every iteration of the loop
Nov 12 '08 #7
Curtis Rutland
3,256 Recognized Expert Specialist
Look at the example loop they use here at the MSDN. Scroll down to the examples section.

Try using the do loop that they used.
Nov 12 '08 #8
Curtis Rutland
3,256 Recognized Expert Specialist
You are also going to want to move the creation of your StreamWriters outside the loop. You don't want to overwrite these files each time, you want to append them.

So create them outside the loop, use their WriteLine methods inside the loop, and then close them after the loop is done.

Always remember to close your streams at the end of a program. Always.
Nov 12 '08 #9
kashif73
91 New Member
InsertAlias & Plater. Many thanks for your valuable inputs. The code works as desired now. the reading at MSDN was really helpful. my file was overwritten each time the loop was run. taking the streamwriters out of the loop does the trick. Thanks again.
Nov 12 '08 #10
Curtis Rutland
3,256 Recognized Expert Specialist
Always glad to help.
Nov 13 '08 #11
kashif73
91 New Member
InsertAlias one more thing please, how can I read the last character of each line & then write it at the end of each line of first split file? e.g
if my text file record is :
Main text file
Line 1: 1;2;3;4;5;6;7;8;9
Line 2: 4;5;6;3;2;6;7;8;1
Split File 1:
Line1: 1;2;3;4;5;9
Line2: 6;7;8;9

Split File 2 :
Line 1: 4;5;6;3;2;1
Line2: 6;7;8;1

SO how do I get 9 & 1 to be written in my first split file also & like wise for other rows?
Thanks.
Nov 20 '08 #12
kashif73
91 New Member
Dont worry I figured it out. Thanks :)
Nov 20 '08 #13
Plater
7,872 Recognized Expert Expert
You have all the split pieces in an array, just pick the index of the one you want and write it first (or last)
Nov 20 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: nieuws | last post by:
Hi, I was trying to do the following. It's my first php "project", so it's quiet logic that i have some problems. Perhaps the php community might help. It's about this : I have a txt file...
7
by: Hans A | last post by:
I have a textfile "textfile.txt" containing a list of words. There is one word on each line. I want to pick two random lines from this textfile, and I have tried to do something like: //Loading...
2
by: Ron | last post by:
I am trying to read a delimited textfile to the console window as follows: using System.IO; StreamReader oRead; string str1; object str2; oRead = new StreamReader(strPath +...
1
by: Krish | last post by:
I have requirement, that i get one big chunk of text file. This text file will have has information, that on finding "****End of Information****", i have to split them individual text file with our...
3
by: Jan Hanssen | last post by:
Hi! I have a list of data in a textfile which is tab delimited. Each line is seperated by a VbCrLf. I want to collect this data in a multidimensional string array. I do not wish to use a...
8
by: Flyzone | last post by:
Hi, i have a problem with the split function and regexp. I have a file that i want to split using the date as token. Here a sample: ----- Mon Apr 9 22:30:18 2007 text text Mon Apr 9...
3
by: DaWofl | last post by:
Iam reading text from a textfile iam splitting it by # at te end of the line so get al lines into my listbox like this aryTextFile = LineOfText.Split("#") For i = 0 To...
1
by: perdoname | last post by:
Hello, Im trying to implement a program which will split a text file and then parses the elements to an arraylist. My text file looks like that: My program is that: public class Parse {
1
by: asedt | last post by:
With my Excel macro and two text files I want to create a new textfile containing the first textfile then text from the sheet and then the second textfile. My problem is that i don't know how to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.