Results 1 to 2 of 2

Thread: Find most recently modified file

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Find most recently modified file

    Hi Guys,

    I'm looking for a way to find the most recently modified file in a directory using vbscript. Basically I need to find the most recent backup log in a directory and I have found that you can use DateDiff to compare two dates so at the moment im just checking each file in the directory to see if it is less than a day since it has last been modified but then obviously this does not work on a monday...because the backup finishes writing to the log file on Friday so when the script comes to run on Monday there is nothing thats been modified within the last 24 hrs.

    Cheers
    Chris

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

    Re: Find most recently modified file

    vb Code:
    1. Set oFS = CreateObject("Scripting.FileSystemObject")
    2.    Set ofp = ofs.GetFolder("C:\Documents and Settings\peter\My Documents\basic\vbs")
    3.    Set ofiles = ofp.Files
    4.  For Each fi In ofiles
    5.     If newest = "" Then newest = fi.Name: Set newfile = fi
    6.    
    7.     If fi.DateLastModified > newfile.DateLastModified Then
    8.     Set newfile = fi
    9.     newest = fi.Name
    10.     End If
    11. Next
    12.  
    13. msgbox newest
    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

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