Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Single Line From A Text File 2

Status
Not open for further replies.

snt

MIS
Jun 19, 2000
104
US
I need to extract a single line of text from a text file and assign it to a variable so I can display it. The problem is that the user will open the file so I can not specify a file name. The line of text is a time stamp that is in the same position in every file that would be opened and starts with time = . Does any one know how I could get the time stamp from the file that the user opens and assign it to a variable?

Much thanks.
 
I'm assuming that the user has some way of specifying, or selecting the file to be opened, and that you can put that pathname in a variable.

If so, then

FoundIt = False
TimeStamp = ""
FileHandle = FreeFile
Open <Pathname> for input as #FileHandle
FoundIt = EOF(FileHandle)
While FoundIt = False
Line Input #FileHandle, DataLine
Position = InStr(DataLine, &quot;time =&quot;)
If Position > 0 then
TimeStamp = Mid(DataLine, Position + 6)
FoundIt = True
Else
FoundIt = EOF(FileHandle)
End If
Wend
Close #FileHandle

Good Luck
 
Thanks for the response.
Can you tell me how FoundIt, TimeStamp ,FileHandle, DataLine and Position are defined?

Thanks
 
Dim FoundIt as Boolean
Dim TimeStamp as String
Dim FileHandle as Integer
Dim DataLine as String
Dim Position as Integer

Good Luck
 
I seem to be having a problem with the pathname. There is variable of fname that is defined as the filename. When I use that, I get a file not found error.
But if I hover the mouse over that piece of code, it says fname=&quot;xxx&quot;. xxx is the actual file I opened. Any suggestions?
 
does fname contain the full pathname of the file, or is it simply the actual filename. You would need to specifiy the full pathname, starting with the drive letter all the way through the file extension in the open statement.

Good Luck
 
Yes that is it.
Thank you very much.
You get a star.
 
And anoher one. Craig, mailto:sander@cogeco.ca

&quot;Procrastination is the art of keeping up with yesterday.&quot;

I hope my post was helpful!!!
 
One more minor thing.
I need to be able to open multiple files each one showing their own timestamp. The way it is now, when a new file is opened, the timestamp on the other open files, changes to the newly opened file.
Is there a way to fix this?
 
Perhaps a two-dimensional array would fit the bill, where the first element contains the filename, and the second element contains the associated timestamp.

Good Luck
 
I want to add that i have made TimeStamp into an array, but still can't see how to get this to work
 
ok - this is what i have:

Code:
FoundIt = False
TimeStamp(i) = &quot;&quot;
FileHandle = FreeFile
Open (fdir + fname) For Input As #FileHandle
FoundIt = EOF(FileHandle)
While FoundIt = False
    Line Input #FileHandle, DataLine
    Position = InStr(DataLine, &quot;time= &quot;)
    If Position > 0 Then
     TimeStamp(i) = Mid(DataLine, Position + 6)
     FoundIt = True
     Else
     FoundIt = EOF(FileHandle)
   End If
Wend
Close #FileHandle

    Graph1(0).NumSets = multilist.ListCount
    For i = 0 To multilist.ListCount - 1
        Graph1(0).Legend(i + 1) = (multilist.List(i) & NL & TimeStamp(i))
the line Graph1(0).Legend(i + 1) = (multilist.List(i) & NL & TimeStamp(i)) is writing a legend in a graph with the file name and the time. I can't figure out how to make the time stay fixed with the file.
I am sorryu but i don't know what a two-dimensional array is.
 
I forgot i also have Dim TimeStamp(10)
 
Dim TimeStamp(10, 2)

FoundIt = False
TimeStamp(i, 1) = &quot;&quot;
TimeStamp(i, 2) = &quot;&quot;
FileHandle = FreeFile
Open (fdir + fname) For Input As #FileHandle
TimeStame(i, 1) = fdir + fname
FoundIt = EOF(FileHandle)
While FoundIt = False
Line Input #FileHandle, DataLine
Position = InStr(DataLine, &quot;time= &quot;)
If Position > 0 Then
TimeStamp(i, 2) = Mid(DataLine, Position + 6)
FoundIt = True
Else
FoundIt = EOF(FileHandle)
End If
Wend
Close #FileHandle

Graph1(0).NumSets = multilist.ListCount
For i = 0 To multilist.ListCount - 1
Graph1(0).Legend(i + 1) = TimeStamp(i + 1, 1) & NL & TimeStamp(i + 1, 2)


Not sure whether or not it should be i or i + 1 in the Legend assignment. Depends on whether you start with 0 or 1 for the first file.

Good Luck
 
Graph1(0).Legend(i + 1) = TimeStamp(i, 1) & NL & TimeStamp(i, 2) produced the data. But when I open another file, the timestamp changes on the previously opened file.
 
The filename also changes. I don't know much about VB but i seems as though
TimeStamp(i, 1) = &quot;&quot;
TimeStamp(i, 2) = &quot;&quot;

Is not really clearing for some reason.
 
How is i originally being set up

FoundIt = False
TimeStamp(i, 1) = &quot;&quot;
TimeStamp(i, 2) = &quot;&quot;

This is from the start of the code to get the time stamp - what is the value of i at this point ?
 
fname = txtfile.Text
Directory.Path = fdir

Need to keep in mind that this is not my code. I have to update company software. The guy who wrote is no longer here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top