Link to home
Start Free TrialLog in
Avatar of JohnDoeSr
JohnDoeSr

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vinnyd79
vinnyd79

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
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
uhh, I meant a sub not a function.
Avatar of JohnDoeSr

ASKER

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!