Such a thing as End Of Variable when reading from var? Like EOF when reading from file?

I'd like to be able to read from a variable, line by line, w/o having to first count the number of lines in the variable.  Loop until EOF works great when reading from a file via line input and it'd be great if I could use something similar to that when reading from a variable (line by line).

I'm asking because I need to "flip" the contents of a .txt file and make the bottom (last entry) the top (first) and vice-versa. The only way I know how to do this is to read the file, line by line and dump it into a new file (or back into its own). Thanks.
JohnDoeSrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
You can split it into an array using something like this:

Dim MyVar as string
Dim arrLines() as string

MyVar = "your variable data ........"

arrLines = Split(MyVar,vbCrLf)

' loop through array
Dim x as Long
For x = 0 to Ubound(arrLines)
   MsgBox arrLines(x)
Next x

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vinnyd79Commented:
You could rebuild the variable backwards then write it back to the file:

Dim x As Long
MyVar = ""
For x = UBound(arrLines) To LBound(arrLines) Step -1
   If MyVar = "" Then
     MyVar = arrLines(x)
   Else
     MyVar = MyVar & vbCrLf & arrLines(x)
   End If
Next x

Open "Myfile.txt" for output as #1
Print #1, MyVar
Close #1
vinnyd79Commented:
Maybe put it into a function:


Private Sub Command1_Click()
FlipFile "C:\MyFile.txt"
End Sub


Private Sub FlipFile(sFileName As String)
Dim ff As Integer, fData As String
Dim arrLn() As String, x As Long
ff = FreeFile
Open sFileName For Input As #ff
fData = Input$(LOF(ff), ff)
Close #ff

arrLines = Split(fData, vbCrLf)

ff = FreeFile
Open sFileName For Output As #ff
For x = UBound(arrLines) To LBound(arrLines) Step -1
    If Trim$(arrLines(x)) <> "" Then
        Print #ff, arrLines(x)
    End If
Next x
Close #ff
End Sub
vinnyd79Commented:
uhh, I meant a sub not a function.
JohnDoeSrAuthor Commented:
Wow, I was ready to accept your first comment but ate lunch first then came back and you added a bunch more useful info. Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.