Results 1 to 21 of 21

Thread: Find file by latest timestamp PLEASE HLP!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    2

    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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find file by latest timestamp PLEASE HLP!!

    try like
    vb Code:
    1. mypath = "C:\test\"
    2. myfile = Dir(mypath & "*.*")
    3. newestfile = mypath & myfile
    4. Do Until myfile = ""
    5.     If FileDateTime(mypath & myfile) > FileDateTime(newestfile) Then newestfile = mypath & myfile
    6.     myfile = Dir
    7. Loop
    8. 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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    2

    Re: Find file by latest timestamp PLEASE HLP!!

    Thanks sooooo much!!!!

    It worked exactly how I want it to!


  4. #4
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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.
    I'm still on the path of learning....

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. open mypath & myfile for input as 1
    2.   text1 = input(lof(1),#1)
    3. 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

  6. #6
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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
    Last edited by cheowkwen; Jan 10th, 2009 at 02:48 AM.
    I'm still on the path of learning....

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:\
    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

  8. #8
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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.
    I'm still on the path of learning....

  9. #9
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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. 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
    I'm still on the path of learning....

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find file by latest timestamp PLEASE HLP!!

    vb Code:
    1. mypath = "C:\Documents and Settings\All Users\Application Data\BVRP Software\mobile PhoneTools\document\"
    2.       myfile = Dir(mypath & "*.SMS*")
    3.       newestfile = mypath & myfile
    4.       Do Until myfile = ""
    5.           If FileDateTime(mypath & myfile) > FileDateTime(newestfile) Then newestfile = mypath & myfile
    6.           myfile = Dir
    7.       Loop
    8.      Open newestfile For binary As 1
    9.       Text1 = Input(LOF(1), #1)
    10.       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

  11. #11
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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) . 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?
    I'm still on the path of learning....

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    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

  13. #13
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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....
    Attached Files Attached Files
    Last edited by cheowkwen; Jan 10th, 2009 at 10:19 AM.
    I'm still on the path of learning....

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. Open "test.txt" For binary As 1
    2.       mystr = Input(LOF(1), #1)
    3.       Close 1
    4.      txtlat.text = Val(Mid(mystr, 5))
    5.      txtlong.text = Val(Mid(mystr, InStr(mystr, "long") + 5))

    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

  15. #15
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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?
    I'm still on the path of learning....

  16. #16
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Find file by latest timestamp PLEASE HLP!!

    Did you use a string as suggested?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  17. #17
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. txtlat = Mid(mystr, 5, InStr(5, mystr, ",") - 5)
    2. txtlong = Mid(mystr, InStr(mystr, "long") + 5, InStr(InStr(mystr, "long") + 5, mystr, ",") - (InStr(mystr, "long") + 5))
    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

  18. #18
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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.
    Last edited by cheowkwen; Jan 11th, 2009 at 02:30 AM.
    I'm still on the path of learning....

  19. #19
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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.
    Last edited by cheowkwen; Jan 11th, 2009 at 09:06 AM.
    I'm still on the path of learning....

  20. #20
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    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

  21. #21
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    Re: Find file by latest timestamp PLEASE HLP!!

    Thanks for your help, westconn1. I will take your advise.
    I'm still on the path of learning....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width