Solved

Delete oldest file in folder using visual basic 6

Posted on 2007-11-19
4
1,329 Views
Last Modified: 2008-02-21
I need to be to count the number of files in a folder using visual basic 6.  If the file count exceeds a per determined number of files I need to the determine which file is the oldest and then delete the oldest file.  Can this be done?  Does anybody have any examples?
0
Comment
Question by:Mike_Stevens
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 20314770
This is set to 10 files.

Sub FindFiles()
    Dim strFileName As String
    Dim strOldestFile As String
    Dim strFolder As String
    Dim iFileCount As Integer
    Dim i As Integer
    Dim dt As Date
   
    strFolder = "C:\MyFolder"
    strFileName = Dir$(strFolder & "\*")
    strOldestFile = strFileName
    dt = FileDateTime(strFolder & "\" & strFileName)
    Do Until strFileName = ""
    Debug.Print strFileName, FileDateTime(strFolder & "\" & strFileName),
        If (FileDateTime(strFolder & "\" & strFileName)) < dt Then
            dt = FileDateTime(strFolder & "\" & strFileName)
            strOldestFile = strFileName
        End If
        i = i + 1
        strFileName = Dir$()
    Loop
    If i > 10 Then
        Kill strFolder & "\" & strOldestFile
    End If
End Sub
0
 

Author Comment

by:Mike_Stevens
ID: 20315023
Awesome...thanks
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20315038
Here's an alternative function for you.  Just add a reference to the Microsoft Scripting Runtime...

Public Sub PurgeFile(ByVal path As String, ByVal numberOfFiles As Integer)

Dim fso As New FileSystemObject
Dim fldr As Folder
Set fldr = fso.GetFolder(path)

Dim fil As File
Dim oldestDate As Date
Dim oldestFile As String
oldestDate = Date  'Set to today's date

If fldr.Files.Count >= numberOfFiles Then
   
  For Each fil In fldr.Files
 
    If fil.DateLastModified < oldestDate Then
        oldestFile = fil.path
    End If
   
  Next
   
  'Delete oldest file:
  fso.DeleteFile oldestFile
  MsgBox oldestFile & " deleted"
 
End If

End Sub

0
 

Expert Comment

by:Naladmin
ID: 20951720
Hello VBRocks, I have found your code very helpful. How in that code, I can specify the extension of the file? I mean, for example, only to delete .txt files, or .pdf files.

Thanks.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question