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.
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.
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.
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.
Thanks InserAlias. As per your suggestion please see my code below: - Dim FILE_NAME As String = "C:\IKN Individual\Ind-" & IDN.Text & ".ikn"
-
If System.IO.File.Exists(FILE_NAME) = True Then
-
Dim lines() As String = File.ReadAllLines(FILE_NAME)
-
-
Dim fs As New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
-
Dim sr As New StreamReader(fs)
-
-
-
Dim LineIn As String
-
While sr.Peek <> -1
-
LineIn = sr.ReadLine()
-
-
'Dim Contents As String = sr.ReadToEnd()
-
Dim textdelimiter As String
-
textdelimiter = ";"
-
Dim splitout = Split(LineIn, textdelimiter)
-
-
Dim splitText() As String = LineIn.Split(";")
-
-
Dim oWrite As System.IO.StreamWriter
-
'Dim oFile As System.IO.File
-
oWrite = IO.File.CreateText("C:\IKN Individual\sample.txt")
-
Dim J As Integer
-
-
For J = 0 To splitText(1000)
-
oWrite.WriteLine(splitText(1000))
-
Next
-
oWrite.Close()
-
-
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.
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. - Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
-
-
-
Dim FILE_NAME As String = "C:\IKN Individual\Ind-" & IDN.Text & ".ikn"
-
If System.IO.File.Exists(FILE_NAME) = True Then
-
-
-
Dim fs As New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
-
Dim sr As New StreamReader(fs)
-
-
Dim LineIn As String
-
-
While sr.Peek <> -1
-
LineIn = sr.ReadLine()
-
Dim textdelimiter As String
-
textdelimiter = ";"
-
Dim splitText() As String = LineIn.Split(";")
-
-
Dim oWrite As System.IO.StreamWriter
-
oWrite = IO.File.CreateText("C:\IKN Individual\sample.txt")
-
Dim J As Integer
-
-
For J = 0 To splitText.Length - 221
-
oWrite.Write(splitText(J) & ";")
-
Next
-
oWrite.WriteLine()
-
oWrite.Close()
-
-
oWrite = IO.File.CreateText("C:\IKN Individual\sample2.txt")
-
Dim G As Integer
-
-
For G = splitText.Length - 222 To splitText.Length - 1
-
oWrite.Write(splitText(G) & ";")
-
Next
-
oWrite.WriteLine()
-
oWrite.Close()
-
-
End While
-
-
'myReader.Close()
-
fs.Close()
-
sr.Close()
-
Else
-
-
MsgBox("The TEXTFILE does Not Exist.")
-
-
End If
-
End Sub
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
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.
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.
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.
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.
Dont worry I figured it out. Thanks :)
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)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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 +...
|
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...
|
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...
| |
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...
|
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...
|
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 {
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |