Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delete oldest file in folder using visual basic 6

Posted on 2007-11-19
4
Medium Priority
?
1,412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Suggested Courses

705 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