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?
Printable View
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?
Try this...
VB Code:
Private Sub Command1_Click() Dim lngPos As Long Dim lineTxt As String Open "c:\sample.txt" For Input As #1 For lngPos = 1 To 25 Line Input #1, lineTxt Next Label1.Caption = lineTxt End Sub
That will return the 25th line of a text file.
You need to read the entire file, make whatever changes you want, then write the modified information back to the 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...
the code zynder posted should help, did you try it?
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:
Dim strLines() As String Dim i As Long Open "sometextfile.txt" For Input As #1 strLines = Split(Input(LOF(1), 1), vbCrLf) Close #1 For i = 0 To UBound(strLines) If this_is_the_line_you_want_to_replace Then strLines(i) = label1.Caption Exit For End If Next i Open "sometextfile.txt" For Output As #1 Print #1, Join(strLines, vbCrLf); ' the semicolon (;) is important Close #1
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?
Try this for the 25th line... although it's a bit long.
VB Code:
Private Sub Command1_Click() Dim lngPos As Long Dim lineTxt As String Dim myString() As String Open "c:\sample.txt" For Input As #1 myString() = Split(Input(LOF(1), #1), vbCrLf) Close #1 Open "c:\sample.txt" For Output As #1 For i = 0 To 23 Print #1, myString(i) Next Print #1, Label1.Caption For i = 26 To UBound(myString) Print #1, myString(i) Next Close #1 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.
VB Code:
For i = 0 To UBound(strLines) If Left(strLines(i), 12) = "Selected car" Then strLines(i) = "Selected car " & Label1.Caption Exit For End If Next i
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.
What am I missing?
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.
oO this looks intreasting, but i cant help. But it has helped me in one way :P
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.
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.
Why not just use Replace() to replace "Selected car xxx" with "Selected car" & Label1.Caption?Quote:
Originally Posted by stevevb6
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?
That's a seriously overkill way of doing:Quote:
Originally Posted by Logophobic
VB Code:
string = Replace(string, "Selected car xxx", "Selected car " & Label1.caption, 1, 1)
WAY overkill.
VB Code:
Dim file As String Open "C:\Test.txt" For Input As #1: file = Input(LOF(1), #1): Close #1 file = Replace(file, "Selected car xxx", "Selected car " & Label1.Caption, 1, 1) 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:
Dim file() As String, i As Integer, count As Integer: count = 0 Open "C:\Test.txt" For Input As #1: file = Split(Input(LOF(1), #1), vbCrLf): Close #1 For i = 0 To UBound(file) If InStr(file(i), "Selected car xxx") > 0 Then count = count + 1 If count = 4 Then 'Find the fourth instance of the phrase newfile = newfile & Replace(file(i), "Selected car xxx", "Selected car " & _ Label1.Caption) & vbNewLine Else newfile = newfile & file(i) & vbNewLine End If Next 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:
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
@BrendanDavis
Points taken. :)
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.Quote:
Originally Posted by scriptman
Easiest way to get a specific line of a string is:
VB Code:
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.
yep i agree
I got it to work, but i gets rid of some other lines in the txt file. I used
VB Code:
Dim file As String Open "D:\cfg.txt" For Input As #1: file = Input(LOF(1), #1): Close #1 file = Replace(file, "Selected Car XF GTI", "Selected car " & Label4.Caption, 1, 1) Open "D:\cfg.txt" For Output As #1: Print #1, file: Close #1
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"
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.Quote:
Originally Posted by Logophobic
thats what i am using and so far it has been working great! thatnks for all the amazing help!
VB Code:
For i = 0 To UBound(strLines) If Left(strLines(i), 12) = "Selected car" Then strLines(i) = "Selected car " & Label1.Caption Exit For End If Next iThere 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.Quote:
Originally Posted by BrendanDavis
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.