Solved

Delete oldest file in folder using visual basic 6

Posted on 2007-11-19
4
1,285 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
Comment Utility
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
Comment Utility
Awesome...thanks
0
 
LVL 27

Expert Comment

by:VBRocks
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now