Results 1 to 16 of 16

Thread: Reorganizing text file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    219

    Reorganizing text file

    hey i have a very large file and well the format SUCKS lol its like
    name | name2 | name3 | name4 | name5 | ect ect

    How can i make it so i can reorganize the list to
    name
    name2
    name3
    name4

    ect ect

  2. #2
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Reorganizing text file

    Are all the strings the same?

    Like is the text file:
    String
    String1
    String2
    String3

    Or are they different:
    String
    Llama1
    Horse9

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Reorganizing text file

    If the names are delimited then it would be relatively easy. Are those bars you showed us actually in the file? If not, what characters are between the names?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    219

    Re: Reorganizing text file

    there different the only thing is the same is
    |
    which seperates them

  5. #5
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Reorganizing text file

    Try using Split?

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Reorganizing text file

    If the bar is in there, then it should be very simple.
    1. Read the text file, either in total or line by line
    2. Replace the bars with carriage returns
    3. Save the text to a new file

    P.S. Using a delimiter is not necessarily a bad file format -- really it is a very usable format. If you think about it, a name on each line is basically the same thing; only the delimiter is a carriage return vs a bar.
    Last edited by LaVolpe; Jan 6th, 2008 at 12:46 AM.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    219

    Re: Reorganizing text file

    how would i go about making a new line once it removes it? because if i just use the split function im going to end up with stringstring1string2string3 ect ect lol

  8. #8
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Reorganizing text file

    strOrig = Replace(strOrig, "|", vbNewLine)
    strOrig = Replace(strOrig, Space(1), vbNullString)

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Reorganizing text file

    Quote Originally Posted by DeCo
    how would i go about making a new line once it removes it? because if i just use the split function im going to end up with stringstring1string2string3 ect ect lol
    Actually Split could be used, but a simpler method, on second thought, might just be to use VB's Replace$(). Read the text and replace | with vbCrLf. Then save the modified text.

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Reorganizing text file

    DeCo: No, you're going to end up with an array of words, each word in a different element of the array
    Code:
    Dim strArray() As String
    strArray = Split("Fred|Joe|Harry" ,"|")
    strArray(0) = Fred
    strArray(1) = Joe
    strArray(2) = Harry

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    219

    Re: Reorganizing text file

    whats strOrig?

  12. #12
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Reorganizing text file

    strOrig = Text File.

  13. #13
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Reorganizing text file

    Quote Originally Posted by LaVolpe
    Actually Split could be used, but a simpler method, on second thought, might just be to use VB's Replace$(). Read the text and replace | with vbCrLf. Then save the modified text.
    One issue is whether there's a terminating "|" at the end of each line. If there is, then Replace will be ok, if not, then you'll get names running into each other.
    eg
    Line 1: Fred!Joe
    Line 2: Harry|Bert

    after Replace (with vbCrLf):
    Fred
    JoeHarry
    Bert

    and if there is a trailing "|" then Split will return an extra element of the array with a null value.
    eg
    Line1: Fred|Joe|
    Line2: Harry|Bert|

    after Split to array:
    array(0) = Fred
    array(1) = Joe
    array(2) = Harry
    array(3) = Bert
    array(4) = null

    Not a major issue, but one to watch out for.

    EDIT: and if there's a mixture then it's back to good old InStr and Mid$
    Last edited by Doogle; Jan 6th, 2008 at 01:32 AM. Reason: Too many nulls

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    219

    Re: Reorganizing text file

    Code:
    Private Sub Form_Load()
    
    Open App.Path & "\1.txt" For Input As #1
    Input #1, strtext
    
    Text1.Text = strtext
    
    End Sub
    
    Private Sub Command1_Click()
    
    strtext = Replace(strtext, "|", vbNewLine)
    strtext = Replace(strtext, Space(1), vbNullString)
    Text1.Text = strtext
    
    End Sub
    when command1 execute's text1 completely erases?

  15. #15
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Reorganizing text file

    Declare strtext in the Declarations section of the form. Vis:
    Code:
    Private strtext As String
    
    Private Sub Form_Load()
    
    Open App.Path & "\1.txt" For Input As #1
    Input #1, strtext
    
    Text1.Text = strtext
    
    End Sub
    
    Private Sub Command1_Click()
    
    strtext = Replace(strtext, "|", vbNewLine)
    strtext = Replace(strtext, Space(1), vbNullString)
    Text1.Text = strtext
    
    End Sub
    But see Post #13

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    219

    Re: Reorganizing text file

    One issue is whether there's a terminating "|" at the end of each line. If there is, then Replace will be ok, if not, then you'll get names running into each other.
    I know thats happening to me atm im trying to fix it lol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width