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.
If FileDateTime(mypath & myfile) > FileDateTime(newestfile) Then newestfile = mypath & myfile
myfile = Dir
Loop
MsgBox newestfile
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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.
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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
Last edited by cheowkwen; Jan 10th, 2009 at 02:48 AM.
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:\
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Hi, i noticed the mistake, westconn1. I accidentally deleted the line (newestfile = mypath & myfile) and now i have corrected it. Thanks a again, westconn1.
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. 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
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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) . 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?
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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....
Last edited by cheowkwen; Jan 10th, 2009 at 10:19 AM.
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
slightly different code would be needed to return strings
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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.
Last edited by cheowkwen; Jan 11th, 2009 at 09:06 AM.
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete