Check if a File name is more than 90 days old, & Delete

Selva V Pasupathy, HSBC Global Resourcing, Hyderabad

Option Explicit
‘___________________________________________________________________________
‘***************************************************************************
‘* MODULE NAME:     CODE TO COMPARE IF ANY FILENAME IS > 90 DAYS OLD
‘* AUTHOR:          Selva V Pasupathy, HSBC Global Resourcing, Hyderabad
‘*
‘*
‘* CONTACT:         socko@rediffmail.com
‘* WEB SITE:        https://socko.wordpress.com
‘* NOTES:
‘*  ————————————————————————
‘*  ————————————————————————
‘*
‘*___________________________________________________________________________
‘***************************************************************************

‘ CODE TO COMPARE IF ANY FILENAME IS > 90 DAYS OLD

Const Files_Location = “J:\Falcon\Management\INFILL REPORTS\FCD\”

Sub Delete_Files(ByRef sDir As String)
  Dim i As Integer
  Dim dt2Delete, strDT As Date
  
  With Application.FileSearch
    .LookIn = sDir
    .SearchSubFolders = False
    .FileType = msoFileTypeExcelWorkbooks
    .Execute
  End With
      
  With Application.FileSearch
    If .Execute() > 0 Then
      MsgBox “There were ” & .FoundFiles.Count & _
          ” file(s) found.”
        For i = 1 To .FoundFiles.Count
          MsgBox .FoundFiles(i)
          strDT = Mid(.FoundFiles(i), _
          InStrRev(.FoundFiles(i), ” “, _
          -1, vbTextCompare) + 1, 255)
          
          dt2Delete = Format(Now() – 90, “ddmmyyyy”)
          MsgBox dt2Delete
          If strDT < dt2Delete Then
              MsgBox strDT
              MsgBox “NEED TO DELETE”
          Else
              MsgBox “NEED NOT DELETE”
          End If
        Next i
      Else
          MsgBox “There were no files found.”
      End If
  End With

End Sub

Sub testDeleteFiles()
Call Delete_Files(Files_Location)
End Sub

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: