Results 1 to 28 of 28

Thread: [RESOLVED] Editing one line of a txt file?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Resolved [RESOLVED] Editing one line of a txt file?

    I was wondering how i can substitute one line of a txt file (not the first line, should be around the 25th line) with the caption of a label?

  2. #2
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Editing one line of a txt file?

    Try this...

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim lngPos As Long
    3. Dim lineTxt As String
    4. Open "c:\sample.txt" For Input As #1
    5.  
    6. For lngPos = 1 To 25
    7.     Line Input #1, lineTxt
    8. Next
    9.     Label1.Caption = lineTxt
    10. End Sub

    That will return the 25th line of a text file.

  3. #3
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Editing one line of a txt file?

    You need to read the entire file, make whatever changes you want, then write the modified information back to the file.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Editing one line of a txt file?

    I want to make the line of the text file = the caption of the label. the line number is actually 52... I underestimated slightly...

  5. #5
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Editing one line of a txt file?

    the code zynder posted should help, did you try it?

  6. #6
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Editing one line of a txt file?

    The code zynder posted puts a line from the text file into the caption of a label. What is needed is the reverse.
    VB Code:
    1. Dim strLines() As String
    2.   Dim i As Long
    3.  
    4.   Open "sometextfile.txt" For Input As #1
    5.     strLines = Split(Input(LOF(1), 1), vbCrLf)
    6.   Close #1
    7.  
    8.   For i = 0 To UBound(strLines)
    9.     If this_is_the_line_you_want_to_replace Then
    10.       strLines(i) = label1.Caption
    11.       Exit For
    12.     End If
    13.   Next i
    14.  
    15.   Open "sometextfile.txt" For Output As #1
    16.     Print #1, Join(strLines, vbCrLf); ' the semicolon (;) is important
    17.   Close #1

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Editing one line of a txt file?

    how would i change that code to select 62nd line, i counted wrong again. lol the line reads
    Selected car xxx
    how can i change only the xxx?

  8. #8
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Editing one line of a txt file?

    Try this for the 25th line... although it's a bit long.

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim lngPos As Long
    3. Dim lineTxt As String
    4. Dim myString() As String
    5.  
    6. Open "c:\sample.txt" For Input As #1
    7.     myString() = Split(Input(LOF(1), #1), vbCrLf)
    8. Close #1
    9.  
    10. Open "c:\sample.txt" For Output As #1
    11. For i = 0 To 23
    12.     Print #1, myString(i)
    13. Next
    14.    
    15.     Print #1, Label1.Caption
    16.    
    17. For i = 26 To UBound(myString)
    18.     Print #1, myString(i)
    19. Next
    20. Close #1
    21.  
    22. End Sub

    This is for editing 25th line only. If you want to change line 62, you need to change the values accordingly. That wasn't the best code but it works.

    [edit] It's so confusing. Do you know the value of the text file or you just want to edit it? If you know the value then Logophobic's code will work.
    Last edited by zynder; Feb 3rd, 2007 at 09:06 PM.

  9. #9
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Editing one line of a txt file?

    VB Code:
    1. For i = 0 To UBound(strLines)
    2.     If Left(strLines(i), 12) = "Selected car" Then
    3.       strLines(i) = "Selected car " & Label1.Caption
    4.       Exit For
    5.     End If
    6.   Next i

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Editing one line of a txt file?

    i want to switch the "xxx" in the example i gave you. The thing is, it could be one of 19 posibilites... And as far as length of the code or the time it takes to complete goes, it doesnt really matter, as long as it works good.

  11. #11
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Editing one line of a txt file?

    What am I missing?

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Editing one line of a txt file?

    i want to make the selected car xr gt turbo = selected car label4.caption. it happens to be the 72nd line. I really cant count anymore! I must be too tired or something.

  13. #13
    Addicted Member
    Join Date
    Aug 2006
    Posts
    208

    Re: Editing one line of a txt file?

    oO this looks intreasting, but i cant help. But it has helped me in one way :P

  14. #14
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Editing one line of a txt file?

    It doesn't matter which line it happens to be. The code I gave will find the first line that begins with "Selected car" and replace the rest of that line with the caption of the label. You will need to change Label1 to Label4, or whatever name you choose for your label. You may also need to change "Selected car" to "selected car", as the comparison is case-sensitive.

  15. #15
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Editing one line of a txt file?

    Be sure it doesn't have any duplicates in the text file for it to work porperly. I'm not sure stevevb6 what you're up to. We have given examples, its' up to you to work around with it.

  16. #16
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Editing one line of a txt file?

    Quote Originally Posted by stevevb6
    how would i change that code to select 62nd line, i counted wrong again. lol the line reads
    Selected car xxx
    how can i change only the xxx?
    Why not just use Replace() to replace "Selected car xxx" with "Selected car" & Label1.Caption?

    Is there more than one line that contains "Selected car xxx"? If so, is the line you want to replace ALWAYS going to be on the 62nd(or whatever) line?
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  17. #17
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Editing one line of a txt file?

    Quote Originally Posted by Logophobic
    VB Code:
    1. For i = 0 To UBound(strLines)
    2.     If Left(strLines(i), 12) = "Selected car" Then
    3.       strLines(i) = "Selected car " & Label1.Caption
    4.       Exit For
    5.     End If
    6.   Next i
    That's a seriously overkill way of doing:

    VB Code:
    1. string = Replace(string, "Selected car xxx", "Selected car " & Label1.caption, 1, 1)

    WAY overkill.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  18. #18
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Editing one line of a txt file?

    VB Code:
    1. Dim file As String
    2.  
    3. Open "C:\Test.txt" For Input As #1: file = Input(LOF(1), #1): Close #1
    4.     file = Replace(file, "Selected car xxx", "Selected car " & Label1.Caption, 1, 1)
    5. Open "C:\Test.txt" For Output As #1: Print #1, file: Close #1

    That'll replace the first instance of "Selected car xxx" and no other, with "Selected car " & the label's caption.

    If your string or file contains several instances of "Selected car xxx" before the one you wish to replace, you can simply do something like:

    VB Code:
    1. Dim file() As String, i As Integer, count As Integer: count = 0
    2.  
    3. Open "C:\Test.txt" For Input As #1: file = Split(Input(LOF(1), #1), vbCrLf): Close #1
    4.  
    5. For i = 0 To UBound(file)
    6.     If InStr(file(i), "Selected car xxx") > 0 Then count = count + 1
    7.     If count = 4 Then 'Find the fourth instance of the phrase
    8.         newfile = newfile & Replace(file(i), "Selected car xxx", "Selected car " & _
    9.         Label1.Caption) & vbNewLine
    10.     Else
    11.         newfile = newfile & file(i) & vbNewLine
    12.     End If
    13. Next
    14.  
    15. Open "C:\Test.txt" For Output As #1: Print #1, Mid(newfile, 1, Len(newfile) - 2): Close #1

    In that example, it will find the 4th instance of "Selected car xxx" and replace only it with "Selected car " & Label1.Caption, and leave the other instances alone. Change the "4" in:

    VB Code:
    1. If count = 4 Then

    ...to whatever number instance you wish to find(26th, 62nd, whatever).

    If all you need to do is replace something that will ALWAYS be on a certain line, then you could use one of the examples already provided by the other users here ;D
    Last edited by BrendanDavis; Feb 4th, 2007 at 01:17 AM.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  19. #19
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Editing one line of a txt file?

    @BrendanDavis

    Points taken.

  20. #20

    Re: Editing one line of a txt file?

    i have learn to be able to manipulate easier a listbox for this kind of situations just enter each line of text as an item and then select the row you want :


    listbox.list ( "numer of line to edit" )

    just a tought

  21. #21
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Editing one line of a txt file?

    Quote Originally Posted by scriptman
    i have learn to be able to manipulate easier a listbox for this kind of situations just enter each line of text as an item and then select the row you want :


    listbox.list ( "numer of line to edit" )

    just a tought
    That is a HORRIBLE route to take. HORRIBLE. Especially if you have several lines in a text file or textbox. Not only do you have to scan through the string/file line by line, but then you have to populate a textbox with the EXACT same information. That's absurd.

    Easiest way to get a specific line of a string is:

    VB Code:
    1. lineOf = Split(string, VbCrLf)(1)

    The "1" meaning line 2(it's 0 based, so if you want line 5 you would use "4"). Replace that with anything you wish.

    One line of code versus several lines & populating a listbox and then more code to get the text of the list's index. The choice is obvious.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  22. #22

    Re: Editing one line of a txt file?

    yep i agree

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Editing one line of a txt file?

    I got it to work, but i gets rid of some other lines in the txt file. I used
    VB Code:
    1. Dim file As String
    2.  
    3. Open "D:\cfg.txt" For Input As #1: file = Input(LOF(1), #1): Close #1
    4.     file = Replace(file, "Selected Car XF GTI", "Selected car " & Label4.Caption, 1, 1)
    5. Open "D:\cfg.txt" For Output As #1: Print #1, file: Close #1
    Last edited by stevevb6; Feb 4th, 2007 at 09:21 AM.

  24. #24
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Editing one line of a txt file?

    No it won't work because his code directly targets the string literal "selected car xxx" I think brandon has misunderstood steve is using xxx in place of a value that is variable, not literally "xxx"
    Chris

  25. #25
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Editing one line of a txt file?

    Quote Originally Posted by Logophobic
    VB Code:
    1. For i = 0 To UBound(strLines)
    2.     If Left(strLines(i), 12) = "Selected car" Then
    3.       strLines(i) = "Selected car " & Label1.Caption
    4.       Exit For
    5.     End If
    6.   Next i
    This code works perfect. If there is duplicates in the file that you want to replace also, then just remove the Exit For, otherwise it will remove the first line that has Selected car on.
    Chris

  26. #26

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: [RESOLVED] Editing one line of a txt file?

    thats what i am using and so far it has been working great! thatnks for all the amazing help!

  27. #27
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Editing one line of a txt file?

    VB Code:
    1. For i = 0 To UBound(strLines)
    2.     If Left(strLines(i), 12) = "Selected car" Then
    3.       strLines(i) = "Selected car " & Label1.Caption
    4.       Exit For
    5.     End If
    6.   Next i
    Quote Originally Posted by BrendanDavis
    That's a seriously overkill way of doing:

    VB Code:
    1. string = Replace(string, "Selected car xxx", "Selected car " & Label1.caption, 1, 1)

    WAY overkill.
    There is a huge difference in what these bits of code do. Using the Replace function requires you to know exactly what is being replaced. It should be obvious that "xxx" represents some unknown text (such as "xr gt turbo" given in post #12 or "XF GTI" in post #23). My code will replace anything following "Selected car ", to the end of the line, with the caption of the label.

  28. #28
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [RESOLVED] Editing one line of a txt file?

    I was unaware that it was going to be anything but "xxx" when I posted what I did. If he had been more clear about what he wants from the start, I would have been able to provide a much more precise answer. He was very vague in what he wanted, so it was logical to assume there would be many different codes provided for what was thought to be what he wanted.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

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