+ Reply to Thread
Results 1 to 3 of 3

VBA to delete files in a file path

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    VBA to delete files in a file path

    Not too sure if im on the correct forum on for this but im using this code on an excel workbook:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
     Dim filesys
        Set filesys = CreateObject("Scripting.FileSystemObject")
        If filesys.FileExists("S:\Shipping Receiving\Extrusion\dbExtrusion Receiving.mdb") Then
           filesys.CopyFile "S:\Shipping Receiving\Extrusion\dbExtrusion Receiving.mdb", "S:\AC 174 Railing Certification Program\Database Backups\Backup of dbExtrusion Receiving " & Month(Now()) & "-" & Day(Now()) & "-" & Year(Now()) & " at " & Hour(Now()) & "-" & Minute(Now()) & ".mdb"
        End If
    End Sub
    The code makes a backup copy of an access database via an excel spreadsheet when a user clicks save. The code works well, the problem now is disc space. i would like to keep only the most recent 50 backups, so i would need the code to check the file path, count the number of files, and delete the older files until the number of files is 50. Is this possible?

  2. #2
    Forum Contributor
    Join Date
    10-19-2012
    Location
    Omaha, Nebraska USA
    MS-Off Ver
    Excel 2010
    Posts
    249

    Re: VBA to delete files in a file path

    Hi phbryan,

    Here is some code that goes through a list of files in a folder and only keeps the 50 most recently saved. The variables "nPath", "extFile" and "LimitFile" are set based on your description above. "nPath" is the directory where the backups reside, "extFile" is the file extension, in this case ".mdb" and "LimitFile" is the number of files to keep, which is 50 based on your description. You can change these variables as necessary if your specs are different than what I assumed.

    The program iterates through all the files and deletes the oldest one each time, so if you have 500 files and are keeping 50, it might take it a while to run. If there are only about 50, it should take only a few seconds. Here is the code:

    Sub DeleteOldFiles()
    
    Dim extFile As String
    Dim LimitFile As Long
    Dim cFile As Long
    Dim nPath As String
    Dim nFile As String
    Dim objFso As Object
    Dim objFiles As Object
    Dim objFile As Object
    Dim delFile As String
    
    ' Set Base Directory
    nPath = "S:\AC 174 Railing Certification Program\Database Backups\"
    
    ' Set Limit of Files in directory
    LimitFile = 50
    
    ' Get File Extension
    extFile = ".mdb"
    
    'Get Count of files in directory
    cFile = 0
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objFiles = objFso.GetFolder(nPath).Files
    For Each objFile In objFiles
       If (InStr(objFile.Name, extFile) > 0) Then cFile = cFile + 1
    Next
    
    If (cFile > LimitFile) Then
       For i = 1 To (cFile - LimitFile)
          nFile = Dir(nPath & "Backup of dbExtrusion Receiving " & "*" & extFile)
          delFile = nFile
          Do While (nFile <> "")
             If (FileDateTime(nPath & delFile) > FileDateTime(nPath & nFile)) Then delFile = nFile
             nFile = Dir()
          Loop
          ' Delete oldest file
          Kill nPath & delFile
       Next i
    End If
    
    ' Clear object variables
    Set objFile = Nothing
    Set objFiles = Nothing
    Set objFso = Nothing
    
    ' Give completion message
    MsgBox "Process Complete."
    
    End Sub
    Hope that helps,

    Dan
    Last edited by djbomaha; 04-21-2016 at 04:11 PM.

  3. #3
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: VBA to delete files in a file path

    Does exactly what i need it to
    Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. how to browse and get the file path of Excel files and place in a cell
    By amethystfeb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-15-2014, 01:34 PM
  2. Finding full file path from list of files in excel
    By fletch82 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-22-2014, 08:45 AM
  3. [SOLVED] VBA - Open Excel Files with file path from Named Ranges
    By bbg22 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-06-2012, 11:41 PM
  4. Return full file path for files in directory
    By jeffreybrown in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-03-2011, 12:42 PM
  5. Search for files, then return file path
    By Carlsbergen in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-06-2010, 09:54 AM
  6. Manage file path when downloading files
    By tbaker897 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-12-2008, 06:24 PM
  7. Replies: 1
    Last Post: 05-24-2007, 07:28 AM
  8. [SOLVED] Using .FoundFiles to list files by path and file name
    By Jon in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-11-2006, 11:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1