Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Jan 2008
Posts: 3
#1: Jan 14 '08
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

kadghar's Avatar
Expert
 
Join Date: Apr 2007
Location: Mexico City
Posts: 1,155
#2: Jan 14 '08

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


Quote:

Originally Posted by kristina1

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
Newbie
 
Join Date: Jan 2008
Posts: 3
#3: Jan 14 '08

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


it works
thank you so much
Member
 
Join Date: Dec 2007
Posts: 41
#4: Jan 14 '08

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


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
Member
 
Join Date: Dec 2007
Posts: 41
#5: Jan 14 '08

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


Or you could parse it in one line of code

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

Torgg
Newbie
 
Join Date: Jan 2008
Posts: 3
#6: Jan 14 '08

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


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.
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#7: Jan 15 '08

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


Quote:

Originally Posted by kristina1

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.
Reply