+ Reply to Thread
Results 1 to 3 of 3

Delete files within a folder

  1. #1
    Registered User
    Join Date
    03-16-2006
    Posts
    3

    Delete files within a folder

    I have within a folder a report worksheet and a macro worksheet that create reports on a daily based. Therefore this folder has tons of reports and I need to add some code to the macro worksheet to delete reports worksheets that are older that 90 days. This is the code that I believe could do the job; Do you have any suggestions or maybe a different approach?

    Dim Filename As String, RecentFile As String, RecentDate As Date
    Dim DelFile As String, Directory As String, myDir As String, OldFile As String

    Sub Delete90DaysOldFiles()

    myDir = "c:\data\excel\reports"
    If Right(myDir, 1) <> "\" Then Directory = myDir & "\"
    Filename = Dir(Directory & "" & "*.xls", 0)
    Do While Filename <> ""
    RecentFile = Filename
    RecentDate = FileDateTime(Directory & Filename)
    If FileDateTime(Directory & Filename) < Date - 90 Then '90 days old
    OldFile = Filename
    End If
    Filename = Dir
    If OldFile <> "" Then
    Kill OldFile
    End If
    Loop

    End Sub

  2. #2
    Tom Ogilvy
    Guest

    Re: Delete files within a folder


    Sub Delete90DaysOldFiles()

    Dim Filename As String, myDir As String, OldFile As String
    Dim DelFile As String, Directory As String,

    myDir = "c:\data\excel\reports"
    If Right(myDir, 1) <> "\" Then Directory = myDir & "\"
    Filename = Dir(Directory & "" & "*.xls", 0)
    Do While Filename <> ""
    ' next two lines not used
    'RecentFile = Filename
    'RecentDate = FileDateTime(Directory & Filename)
    If FileDateTime(Directory & Filename) < Date - 90 Then '90 days old
    OldFile = Filename
    End If
    Filename = Dir
    If OldFile <> "" Then
    Kill OldFile
    End If
    ' reinitialize OldFile
    OldFile = ""
    Loop

    End Sub

    Since you are changing the directory within a DIR loop, you could have
    problems, but as long as you don't then it appears like it should work.


    You might want to make a backup of your directory until you are sure it is
    working correctly.
    --
    Regards,
    Tom Ogilvy


    "tarzan538" <tarzan538.24s9am_1142547003.8233@excelforum-nospam.com> wrote
    in message news:tarzan538.24s9am_1142547003.8233@excelforum-nospam.com...
    >
    > I have within a folder a report worksheet and a macro worksheet that
    > create reports on a daily based. Therefore this folder has tons of
    > reports and I need to add some code to the macro worksheet to delete
    > reports worksheets that are older that 90 days. This is the code that I
    > believe could do the job; Do you have any suggestions or maybe a
    > different approach?
    >
    > Dim Filename As String, RecentFile As String, RecentDate As Date
    > Dim DelFile As String, Directory As String, myDir As String, OldFile As
    > String
    >
    > Sub Delete90DaysOldFiles()
    >
    > myDir = "c:\data\excel\reports"
    > If Right(myDir, 1) <> "\" Then Directory = myDir & "\"
    > Filename = Dir(Directory & "" & "*.xls", 0)
    > Do While Filename <> ""
    > RecentFile = Filename
    > RecentDate = FileDateTime(Directory & Filename)
    > If FileDateTime(Directory & Filename) < Date - 90 Then '90 days old
    > OldFile = Filename
    > End If
    > Filename = Dir
    > If OldFile <> "" Then
    > Kill OldFile
    > End If
    > Loop
    >
    > End Sub
    >
    >
    > --
    > tarzan538
    > ------------------------------------------------------------------------
    > tarzan538's Profile:

    http://www.excelforum.com/member.php...o&userid=32544
    > View this thread: http://www.excelforum.com/showthread...hreadid=523312
    >




  3. #3
    Registered User
    Join Date
    03-16-2006
    Posts
    3
    Thank you for your feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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