Solved

Delete oldest file in folder using visual basic 6

Posted on 2007-11-19
4
1,298 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

867 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

16 Experts available now in Live!

Get 1:1 Help Now