By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,189 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,189 IT Pros & Developers. It's quick & easy.

how to separate first and second 8 characters from line and store it in new txt file

P: 3
hi
i have one txt file that contain data like this

11111111,22222222
22222222,33333333
33333333,44444444


what im trying to do is to make 2 new txt files and to store first 8 characters into first txt and second 8 chars into second txt ... " , " should be excluded

can anyone help me with this

thanks in advice
kristina
Jan 14 '08 #1
Share this Question
Share on Google+
6 Replies


kadghar
Expert 100+
P: 1,295
hi
i have one txt file that contain data like this
...
can anyone help me with this

thanks in advice
kristina
Hi Kristina,

sure, use the MID function to take the part of the string you want
lets say you have Srt1 and Str2 as strings, and

Str1= 12345678,12345678

with MID:

Strt2= mid(str1,1,8) '(base string, starting position, length)

you will have Str2= 12345678
other example:

Str3 = ("hello world",3, 5)

will make Str3 = "llo w"

a more general way will be using also INSTR

HTH
Jan 14 '08 #2

P: 3
it works
thank you so much
Jan 14 '08 #3

P: 41
I think this should get you what you want.


Expand|Select|Wrap|Line Numbers
  1.         Dim file_stream As IO.FileStream
  2.         Dim bytes As Byte()
  3.         Dim myArray() As String
  4.         Dim strLine As String
  5.  
  6.         Dim stream_reader = New System.IO.StreamReader("c:\test0.txt") ' Change path to your source file
  7.         While stream_reader.Peek() >= 0
  8.             strLine = stream_reader.readline()
  9.             myArray = Split(strLine, ",")
  10.  
  11.             file_stream = New IO.FileStream("c:\test1.txt", IO.FileMode.Append) 'Change path to the file for the first value
  12.             bytes = New System.Text.UTF8Encoding().GetBytes(myArray(0) & vbCrLf)
  13.             file_stream.Write(bytes, 0, bytes.Length)
  14.             file_stream.Close()
  15.  
  16.             file_stream = New IO.FileStream("c:\test2.txt", IO.FileMode.Append) 'Change path to the file for the second value
  17.             bytes = New System.Text.UTF8Encoding().GetBytes(myArray(1) & vbCrLf)
  18.             file_stream.Write(bytes, 0, bytes.Length)
  19.             file_stream.Close()
  20.         End While
  21.  
  22.         MessageBox.Show("Done")
Torgg
Jan 14 '08 #4

P: 41
Or you could parse it in one line of code

Dim arrayValues() As String = Split("33333333,44444444", ",")
MsgBox(arrayValues(0))
MsgBox(arrayValues(1))

Torgg
Jan 14 '08 #5

P: 3
I done it and there is the code

Expand|Select|Wrap|Line Numbers
  1. Dim str1 As String
  2. Dim str2 As String
  3. Dim str3 As String
  4. Open "c:\1a.pair" For Input As #1     'original file
  5. Open "c:\11.pair" For Output As #2  'new file
  6. Open "c:\12.pair" For Output As #3
  7. Do Until EOF(1) 'read in file until done
  8.   Line Input #1, str1
  9.   str2 = Mid(str1, 1, 32)
  10.   str3 = Mid(str1, 34, 32)
  11.   Print #2, str2
  12.   Print #3, str3
  13. Loop
  14. Close #1
  15. Close #2
  16. Close #3
Thanks once again.
Jan 14 '08 #6

Expert 5K+
P: 8,434
str2 = Mid(str1, 1, 32)
str3 = Mid(str1, 34, 32)
I see the specs changed somewhat, along the way. This is where the Split() function would have an advantage, of course - you wouldn't need to recode if the length of the fields changes.
Jan 15 '08 #7

Post your reply

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