Find file by latest timestamp PLEASE HLP!!
I have a 3rd party program that creates JPG files with random names, and I would like to find the JPG file that was created last. I imagine that I will need to do this by the timestamp/datestamp associated with the JPG? Can anyone tell me the best way to find the last file created using VB6? All of the files are in one directory, and all of the jpegs are in some sort of number format (ex. 1.jpg or 2351.jpg) but not sequential.
Thanks
Re: Find file by latest timestamp PLEASE HLP!!
try like
vb Code:
mypath = "C:\test\"
myfile = Dir(mypath & "*.*")
newestfile = mypath & myfile
Do Until myfile = ""
If FileDateTime(mypath & myfile) > FileDateTime(newestfile) Then newestfile = mypath & myfile
myfile = Dir
Loop
MsgBox newestfile
Re: Find file by latest timestamp PLEASE HLP!!
Thanks sooooo much!!!!
It worked exactly how I want it to!
:thumb: :thumb:
Re: Find file by latest timestamp PLEASE HLP!!
Hi, westconn1. I not really understand how it works actually, can you explain it to me, please? I just getting a msgbox showing the path of the file only. I doing a similar program, finding the latest modified text file and extract the contains of it. But my text file's name is not fixed, it has the format of rec0001, rec0002 and so on. The number is depend on the latest modified (2 is more latest). By the way, the text file actually has the extension of .SMS but it is viewable by using the notepad.
Re: Find file by latest timestamp PLEASE HLP!!
what the code does is check all files in a directory (mypath) and put the newest file name in a message box
if you only want specific files (.sms) change to
myfile = Dir(mypath & "*.sms")
then instead of messagebox, use something like
vb Code:
open mypath & myfile for input as 1
text1 = input(lof(1),#1)
close 1
where text1 is a multiline textbox or richtext
all depends what you actually want to do
if you want to open notepad search on shell
Re: Find file by latest timestamp PLEASE HLP!!
Hi, westconn1. It works. Thanks a lot. By the way, i have to change the file extension to the .txt (in fact it is .SMS) to make it works. Now i have to work on in extracting the contains (numeric) of the text file. Hopefully it can works. Thanks again.
Code:
Private Sub Command1_Click()
mypath = "C:\Documents and Settings\All Users\Application Data\BVRP Software\mobile PhoneTools\document\"
myfile = Dir(mypath & "*.txt*")
newestfile = mypath & myfile
Open mypath & myfile For Input As 1
Do Until myfile = ""
If FileDateTime(mypath & myfile) > FileDateTime(newestfile) Then newestfile = mypath & myfile
myfile = Dir
Loop
Text1 = Input(LOF(1), #1)
Close 1
End Sub
Re: Find file by latest timestamp PLEASE HLP!!
you did put the part to open the file in the wrong place, and deleted a line that was there (newestfile = mypath & myfile), so possibly, that is the problem, other than that, i have to assume you did check if there are some .txt files in d:\
Re: Find file by latest timestamp PLEASE HLP!!
Hi, i noticed the mistake, westconn1. I accidentally deleted the line (newestfile = mypath & myfile) and now i have corrected it. Thanks a again, westconn1.
Re: Find file by latest timestamp PLEASE HLP!!
Hi, westconn1. The code actually works for text file. But just now i noticed that it reads the contents of the textfile but not my .SMS file. I was misinterpreted the output is my .SMS contains. In fact it is the .txt file in that folder as well. Sorry for wrong information due to my careless mistake. :o Now i try to change it to .SMS extension, it gives error : Run time error '62' Input pass end of file. What is the problem?
Code:
Private Sub Command1_Click()
mypath = "C:\Documents and Settings\All Users\Application Data\BVRP Software\mobile PhoneTools\document\"
myfile = Dir(mypath & "*.SMS*")
newestfile = mypath & myfile
Open mypath & myfile For Input As 1
Do Until myfile = ""
If FileDateTime(mypath & myfile) > FileDateTime(newestfile) Then newestfile = mypath & myfile
myfile = Dir
Loop
Text1 = Input(LOF(1), #1)
Close 1
End Sub
Re: Find file by latest timestamp PLEASE HLP!!
vb Code:
mypath = "C:\Documents and Settings\All Users\Application Data\BVRP Software\mobile PhoneTools\document\"
myfile = Dir(mypath & "*.SMS*")
newestfile = mypath & myfile
Do Until myfile = ""
If FileDateTime(mypath & myfile) > FileDateTime(newestfile) Then newestfile = mypath & myfile
myfile = Dir
Loop
Open newestfile For binary As 1
Text1 = Input(LOF(1), #1)
Close 1
change the code like this, it should resolve that error, but i don't know what you will get in your text1, depends on the content of the file
Re: Find file by latest timestamp PLEASE HLP!!
Yeah, it works and no more error. It can shows the contents in the .SMS file in the textbox. (This time i have checked, no more reading from text file) :D . By the way, may i know what is this line means: Open newestfile For binary As 1? Can i extract the contents (numeric) in .SMS file as what .txt file does?
Re: Find file by latest timestamp PLEASE HLP!!
opening for binary avoids the error of input past end of file, which is generated by a end of file character in the text file, when opened for input
yes you can extract whatever you want from the contents of the file
1 Attachment(s)
Re: Find file by latest timestamp PLEASE HLP!!
Thanks for your information, westconn1. I have one more question, can you show me how to extract certain number in the textfile? I have do a search but most of the threads are taking about extracting the string. May i know is it possible to extract number? I have uploaded my text file as an example. I wish to extract the first two number in the text file (1.462203 and 110.433340) and show in the two different text box. Need help, please....
Re: Find file by latest timestamp PLEASE HLP!!
if you return the numbers as numerics any trialing zeros will be removed, if you want to keep trailing zeros you would have to return the numbers as strings
vb Code:
Open "test.txt" For binary As 1
mystr = Input(LOF(1), #1)
Close 1
txtlat.text = Val(Mid(mystr, 5))
txtlong.text = Val(Mid(mystr, InStr(mystr, "long") + 5))
slightly different code would be needed to return strings
Re: Find file by latest timestamp PLEASE HLP!!
Hi, westconn1. I have gone through the code and it works, it is easy as well. However, i like to keep the trailing zeroes for both lat and long and i have tried many ways like assign it as Str$, setting the length of Mid function but all of these can't work. Is there other way to do this?
Re: Find file by latest timestamp PLEASE HLP!!
Did you use a string as suggested?
Re: Find file by latest timestamp PLEASE HLP!!
returning string is a bit more complicated, as you have to get the end of the string as well
vb Code:
txtlat = Mid(mystr, 5, InStr(5, mystr, ",") - 5)
txtlong = Mid(mystr, InStr(mystr, "long") + 5, InStr(InStr(mystr, "long") + 5, mystr, ",") - (InStr(mystr, "long") + 5))
Re: Find file by latest timestamp PLEASE HLP!!
Hi, westconn1. Thanks a lot. I did try a similar approach as to return it as string, but i just missed out something. Anyway, thanks again.
Edit: I will come back here to rate you, westconn1. Since for the moment, it won't allow me to do so.
Re: Find file by latest timestamp PLEASE HLP!!
Hi, i have a simple question, for the length of the Mid$ function why i can't just put the number instead of using "InStr(5, mystr, ",") - 5"? Since i will know what is the length of the value.
Edit: My main program using the Option Explicit, therefore i have declare the mypath, myfile and etc. These are my declaration:
Dim mypath As String
Dim myfile As String
Dim newestfile As String
Dim mystr As String
Are they correct? Somehow it can works.
Re: Find file by latest timestamp PLEASE HLP!!
your declares look right, but you can always give your variables meaningful names, this will make editing your code easier at any time in the future, or easier for anyone else reading your code
if you believe you always know the length of the values you can just use that, but even in your example the lengths of the values are different, one is 8 the other 10
Re: Find file by latest timestamp PLEASE HLP!!
Thanks for your help, westconn1. I will take your advise. :)