Reading specific lines of a text file

rjwebgraphix

Well-known Member
Joined
May 25, 2010
Messages
590
I'm trying to read 5 specific lines of a file and put them into a 5 different variables. The first one gets the correct line, but the other 4 are getting data that are not the lines I'm trying to read.


Data for Line 20 returns line 20
Data for Line 21 returns line 23
Data for Line 23 returns line 27
Data for Line 26 returns line 32 (beyond the for i limitations)
Data for Line 27 returns line 35 (beyond the for i limitations)


I added a row count just to double check the i count to the row and it returns the same row and i for each line even though the line it's reading is off. This is one of those files that if you open it in notepad it looks all jacked up, but if you open it in a good editor like notepad++ it looks correct, so I'm not sure if there's something in it that is throwing it off. The file is written out by another program and I have no control in how it is written, so it does need to be worked with as it is.


I will need to trim it also for the actual data within each line that needs to be in the variable, but I haven't done that yet. Maybe there's a better way to do it or maybe the file doesn't read the same. It is a config file, so maybe there is a better way of obtaining the needed information from the file.

Where I'm using a for loop in this case, I originally had it as a Do until end of file. and had the same result. I only changed it to a for loop after a msgbox had me stuck for a bit and wanted to cut down on msgbox in loop issues on a file 400+ lines long. :)

As always, any help is greatly appreciated.


Text File to be read
https://drive.google.com/open?id=0B8_pR8z6W9n8WmJSQmViX2xQZHc


Code:
Dim ObjFSO
Dim ObjFile
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFile = ObjFSO.OpenTextFile ("E:\BMX\1MGSV\SteamReg\localconfig.vdf", 1)


Dim User1
Dim User2
Dim User3
Dim User4
Dim StmID


Dim Row
row = 1
for i = 1 to 30
	If i = 20 then
		User1 = ObjFile.Readline
		Msgbox "Hit Row 20 Username1: " & User1 & " " & i & " " & row
		ObjFile.SkipLine
	End If
	If i = 21 then
		StmID = ObjFile.Readline
		Msgbox "Hit Row 21 ID: " & StmID & " " & i & " " & row
		ObjFile.SkipLine
	End If
	If i = 23 then
		User2 = ObjFile.Readline
		Msgbox "Hit Row 23 Username2: " & User2 & " " & i & " " & row
		ObjFile.SkipLine
	End If
	If i = 26 then
		User3 = ObjFile.Readline
		Msgbox "Hit Row 26 Username3: " & User3 & " " & i & " " & row
		ObjFile.SkipLine
	End If
	If i = 27 then
		User4 = ObjFile.Readline
		Msgbox "Hit Row 27 Username4: " & User4 & " " & i & " " & row
		ObjFile.SkipLine
	End If
	If i <> 20 or i <> 21 or i <> 23 or i <> 26 or i <> 27 then
		ObjFile.SkipLine
	End If	
	row = row + 1
next


ObjFile.Close


msgbox "Username1:" & User1 & " " & vbNewLine & _
		"Username2:" & User2 & " " & vbNewLine & _
		"Username3:" & User3 & " " & vbNewLine & _
		"Username4:" & User4 & " " & vbNewLine & _
		"StmID:" & StmID
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Just a stab in the dark but doesn't .Readline advance the pointer? Have you tried removing all the .Skipline commands?

It seems odd that as you do each pair of .Skiplines, the error increases by two lines.

Also, I think the test i <> 20 or i <> 21 or i <> 23 or i <> 26 or i <> 27 is incorrect... surely that will always evaluate to TRUE? I think those ORs should be ANDs.

Single-stepping through the code with F8 will probably help you spot the error.
 
Last edited:
Upvote 0
Just a stab in the dark but doesn't .Readline advance the pointer? Have you tried removing all the .Skipline commands?

That's what I thought too, but then what should be row 21 is one of the rows that is just a {, same with row 23. What should be 26 is row 29; and what should be row 27 is actually row 31.

Well, it actually skipped less that way. I'll have another stab at it today with a fresh mind.
 
Upvote 0
Look at that test too - I'm sure it should be AND rather than OR.
 
Upvote 0
Look at that test too - I'm sure it should be AND rather than OR.

That's funny because I had "and" at first, but obviously I may have had something else off which made me second guess it. In rethinking it, which if I would have done this last night It could have been done and overwith, but that's what you get for banging your head till 4am.......

Just throw it all into an array simplistically then the array count is the line count... It worked first try.... Although now I wonder which way is sloppier because now I have an array that I really don't need. Although it's not a complicated script in the grand scope of things, so shouldn't take up much of anything to have an extra array.

Code:
Dim ObjFSO
Dim ObjFile
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFile = ObjFSO.OpenTextFile ("E:\BMX\1MGSV\SteamReg\localconfig.vdf", 1)


Dim User1
Dim User2
Dim User3
Dim User4
Dim StmID
Dim arrFileRead()


for i = 1 to 30
Redim Preserve arrFileRead(i)
arrFileRead(i) =  ObjFile.Readline
next


for i = lbound(arrFileRead) to ubound(arrFileRead)
    If i = 20 then
        User1 = arrFileRead(i)
    End If
    If i = 21 then
        StmID = arrFileRead(i)
    End If
    If i = 23 then
        User2 = arrFileRead(i)
    End If
    If i = 26 then
        User3 = arrFileRead(i)
    End If
    If i = 27 then
        User4 = arrFileRead(i)
    End If
next


ObjFile.Close


msgbox "Username1:" & User1 & " " & vbNewLine & _
        "Username2:" & User2 & " " & vbNewLine & _
        "Username3:" & User3 & " " & vbNewLine & _
        "Username4:" & User4 & " " & vbNewLine & _
        "StmID:" & StmID
 
Last edited:
Upvote 0
One last thing on this. I'm so close, but always have difficulty manipulating strings.

We'll just use the First User1 var as an example. this is what I have now.....

Code:
User1 = Trim(Replace(arrFileRead(i), vbTab, " "))

Which now Produces

"word" "anotherword"

What I actually need in the var is "anotherword" without the quotes

Although for var StmID the same trim and replace is just a single word with quotes and I need it without quotes.

In both cases, it's also the last word I need without the quotations.
 
Upvote 0
Code:
User1 = Replace(Trim(Replace(arrFileRead(i), vbTab, " ")),"""","")
 
Upvote 0
Code:
User1 = Replace(Trim(Replace(arrFileRead(i), vbTab, " ")),"""","")

That works for the single word, I already though about that, but the User variables have 2 words and I only need the 2nd word in the var.

Edit: Nevermind, I found a function for obtaining the last word that will work....

Code:
Function LastWord(ByVal strInput)
Dim intMyPosition
' Remove trailing spaces if any
strInput = Trim(strInput)
'find position of last space
intMyPosition = InStrRev(strInput, " ")
'get everything to the right of the space
LastWord = Right(strInput, Len(strInput) - intMyPosition)
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top