Results 1 to 5 of 5

Thread: Deleting files based on when they were created. Keeping 10 most recent files.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    11

    Deleting files based on when they were created. Keeping 10 most recent files.

    What I'm trying to do is to delete all files from a directory except for the 10 most recent files. Is there an easy way to do this (probably using some functions I'm unaware of)?. If it's easier then starting off with a large number of files, the directory could start off just containing 10 files, then the oldest file would be deleted when a new one was added. Alternatively it could start out empty and build up to 10 files. All would be acceptable solutions.

    The obvious brute force, but slow and inelegant, route would be to read the directory for the file names, then get the creation date/time of each file and sort them in chronological order, then find the most recent file, then find the 10th most recent file (if there is one), then delete anything that had a creation date/time older then that. This sounds like it may be a very clumsy (and slow) way to do things. I'm not very experience with VB6 and certainly don't know all the available commands and functions. So before blundering off on the wrong path, using the wrong tools and the wrong algorithms, I thought I'd ask if there's a better, smarter, faster and easier way to do this!

    I'm using VP6 (SP5).

    Thanks in advance for any suggestions

  2. #2
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,053

    Re: Deleting files based on when they were created. Keeping 10 most recent files.

    I wouldnt say thats the clumsy or slow way, pretty much what is required if based on file time.

    If the last used files are always coming from your app, then you could keep a list of recently used files.
    You push the newest one onto the top of the list, and pop the oldest one off the end of the list if the max history
    count is exceeded. Accessing a file from the list moves it to the top and removes the old entry. If files are deleted
    they are auto purged on load from the list.

    I use a mechanism such as this to keep an Open -> Recent Files menu always loaded and up to date. Sounds like a
    slightly different use case though

    If you control file name and creation you could just name them all sequentially 1,2,3 etc. save a couple steps checking time and then datediffing them all
    Last edited by dz32; Nov 3rd, 2021 at 10:31 PM.

  3. #3
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    Re: Deleting files based on when they were created. Keeping 10 most recent files.

    There are a few factors that will determine what a "most recent" file is - the created date, the last write/modified date, and the last read date. So the first step will be to determine what you consider a "most recent" file to be.

    Let's assume that the last write/modified date is the value to be used. If you have vbRichClient5.dll or RC6.dll registered on your computer, then the following function will be useful

    Code:
    Sub DeleteAllFilesFromFolder(ByVal p_FolderPath As String, Optional ByVal p_SkipTopX As Long, Optional ByVal p_SortMode As DirListSortMode = dlSortByLastWriteTime, Optional ByVal p_SortAscending As Boolean = False)
       Dim ii As Long
       Dim l_Start As Long
       Dim l_End As Long
       Dim l_Step As Long
       
       With New_c.FSO.GetDirList(p_FolderPath)
          .SortMode = p_SortMode
             
          If p_SortAscending Then
             l_Start = 0
             l_End = .FilesCount - 1
             l_Step = 1
          Else
             l_Start = .FilesCount - 1
             l_End = 0
             l_Step = -1
          End If
          
          For ii = l_Start To l_End Step l_Step
             If ii > (p_SkipTopX - 1) Then
                If p_SortAscending Then New_c.FSO.DeleteFile .Path & .FileName(ii)
             Else
                If Not p_SortAscending Then New_c.FSO.DeleteFile .Path & .FileName(ii)
             End If
          Next ii
          
       End With
    End Sub
    You can call it like so:

    Code:
    DeleteAllFilesFromFolder "PATHTOFOLDER", 10
    NOTE: The above code has only been lightly tested, so make sure to run your own tests against files/folders where you aren't afraid to lose important data!

    If you don't have vbRichClient5.dll or RC6.dll registered on your computer, and it sounds interesting to you, go to vbrichclient.com to download it.

    If you don't want to use a third-party library, then you can code it in vanilla V6 and the steps you described should work fine. Just remember that no matter which approach you use, test, test, and test again on data you aren't afraid to lose!

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Deleting files based on when they were created. Keeping 10 most recent files.

    Quote Originally Posted by bob046 View Post
    What I'm trying to do is to delete all files from a directory except for the 10 most recent files.
    Is there an easy way to do this (probably using some functions I'm unaware of)?
    Your problem becomes easy to solve, when you get a List of Files from a Directory,
    which are handed out to you, already sorted by a criterion of your choice.
    (in your case, "SortByCreationTime" or perhaps "SortByLastWriteTime").

    The vbRichClient-lib offers such a functionality over its cDirList-class:

    Code:
      Dim DL As cDirList, i As Long
      Set DL = New_c.FSO.GetDirList("c:\temp\TestMaxTen", dlSortByCreationTime, "*.txt")
      
      Const MostRecentCount = 10
    
      For i = 0 To DL.FilesCount - 1 - MostRecentCount
          New_c.FSO.DeleteFile DL.Path & DL.FileName(i)
      Next
    So the principle is quite simple - you just leave out the last 10 members of the sorted list
    (specified in your For-Loops "Upper-Bound").

    If your DirList contains 11 sorted Files, the expression which calculates the upper bound of the loop will give:
    11 - 1 - 10 = 0
    So your loop in this case goes from 0 To 0 (exactly one time),
    only deleting the file at index 0, which is the oldest of the sorted 11 files.

    If your DirList.FilesCount is only 10 (or less) then the upper-bound calculation will give negative values -
    meaning the "inner-loop-code" will not be entered at all (and nothing will be deleted).

    HTH

    Olaf

    P.S. ... Ok, Jason beat me to it...

  5. #5
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Deleting files based on when they were created. Keeping 10 most recent files.

    Code:
    Public mylist(1 to 10) as String
    
    Sub AddFile(ByVal Filename$)
        Dim i&
    
        if mylist(1) <> vbNullString then kill mylist(1)
    
        For i = 1 to 9
        mylist(i) = mylist(i+1)
        Next i    
    
        mylist(10) = Filename
    End Sub
    so, quite easy function that will "store" the last 10 and also start deleting when its all filled up.
    you will need to load and save the mylist so it will always be up to date.

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