3 Replies - 765 Views - Last Post: 12 May 2014 - 06:43 AM Rate Topic: -----

#1 hesoyam_2709   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-May 14

Copy specific para from a text file and append it in another using VB

Posted 12 May 2014 - 05:53 AM

Hi All,
I have a text file (Text1.txt) which contains data as below:
(JOB identifier .....
.......<some content here>....
..............................
(/JOB)
(JOB identifier .....
.......<some content here>....
..............................
(/JOB)
<repeated multiple times>

I want to copy the contents of one job i.e. from (JOB identifier) to (/JOB) and append it to another text file say, Text2.txt and continue appending till last (/JOB) is found.

I want to do this through VB.net on a button click. Please help.

Is This A Good Question/Topic? 0
  • +

Replies To: Copy specific para from a text file and append it in another using VB

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Copy specific para from a text file and append it in another using VB

Posted 12 May 2014 - 05:56 AM

What have you tried so far in your attempt to achieve this?
Was This Post Helpful? 0
  • +
  • -

#3 hesoyam_2709   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-May 14

Re: Copy specific para from a text file and append it in another using VB

Posted 12 May 2014 - 06:01 AM

View PostCharlieMay, on 12 May 2014 - 05:56 AM, said:

What have you tried so far in your attempt to achieve this?


I am able to count the total number of (/JOB) in the text file using:
Dim text = IO.File.ReadAllText(path)
Dim total = Regex.Matches(text, "</Job>").Count


and created the new text file where data is to be copied and wrote some random text in it using:
IO.File.Create(fileName).Dispose()
Dim objWriter As New System.IO.StreamWriter(fileName)
objWriter.Write(some random text)


I am not able to think for logic of reading the specific block from the source text file.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Copy specific para from a text file and append it in another using VB

Posted 12 May 2014 - 06:43 AM

is this something you created or does it have general format (ie, HTML, XML, etc...)? If you can get this information into an XMLDocument or HTMLDocument, it should be easy to get at this information. Otherwise you could parse through the string using IndexOf (to find the <JOB and </JOB> nodes) and SubString to get each block passing the values from the IndexOf results as starting point and length from their difference)

This post has been edited by CharlieMay: 12 May 2014 - 06:45 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1