I’m new to XOJO so my learning curve is steep.
I’m trying to extract a date from a text file and save the date as an variable so I can can compare it to another date. I have been able to open the file and can get a message box to display the date but it does not save as a variable.
Dim file As FolderItem = GetFolderItem("sample.txt")
Dim D as string
If file <> Nil Then
If file.exists then
Dim UpdateDateCheck As TextInputStream
Try
UpdateDateCheck = TextInputStream.Open(file)
msgbox UpdateDateCheck.ReadAll
D = UpdateDateCheck.ReadLine
Break
UpdateDateCheck.Close
Catch e as IOException
UpdateDateCheck.Close
MsgBox("Error accessing file.")
End Try
Else
MsgBox("File does not Exist.")
End If
Else
MsgBox("File is empty.")
End If
When I look at the value after the break D has no value.
What am I missing?
“ReadAll” has already read all the data from the text file so there is nothing to read from this file with ReadLine.
“ReadLine” in to D and then display that in MsgBox.
Franck, first you need to extract the date from the string, by using the method mid(), len() or right(). The choice between the three methods mostly depends on where the string containing the date is stored inside the text file. Next you should use the parsedate() method to convert the date from string format to date format. At this point you have a date that can be used as a date. I mean you can do difference, you can have different kind of date formats etc etc. take a look at those method inside the language reference.
dim mystring as string // this variable will cointain the string where the date is stored
dim mydate as new date // create a date object
dim checkdate as boolean // to store the result after checking if the string is a date or not
mystring ="today is 06/04/2014" // the sample string to extract the date from
checkdate =parsedate(mid(mystring, 10, 10), mydate) // if mystring is a date, the date is assigned to mydate
MsgBox mydate.LongDate //shows the long date format
Gentlemen,
Thank you both for the quick reply, I knew it was something silly and was obviously too close to see my mistake.
I already had the parsedate code done but was not getting the data from the file. Antonio, thanks for the code, it looks very similar to what I had so I was on the right path. Next for me is to take this information and either make a button active or not, wish me luck.
Thanks,
Frank