Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Delete files within a folder


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

Dim Filename As String, RecentFile As String, RecentDate As Date
Dim DelFile As String, Directory As String, myDir As String, OldFile A
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 Su

--
tarzan53
-----------------------------------------------------------------------
tarzan538's Profile: http://www.excelforum.com/member.php...fo&userid=3254
View this thread: http://www.excelforum.com/showthread.php?threadid=52331

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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" wrote
in message ...

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Delete files within a folder


Thank you for your feedback

--
tarzan53
-----------------------------------------------------------------------
tarzan538's Profile: http://www.excelforum.com/member.php...fo&userid=3254
View this thread: http://www.excelforum.com/showthread.php?threadid=52331

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven Excel Discussion (Misc queries) 1 January 24th 06 03:28 PM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven[_2_] Excel Programming 1 January 24th 06 04:23 AM
Delete All Files in a folder Michael Smith Excel Programming 1 June 8th 05 05:32 PM
delete all the contents (sub folders and files) in the temp folder Joseph Excel Discussion (Misc queries) 0 June 6th 05 08:01 AM
Delete all files within a folder (incl subfolders) Steph[_3_] Excel Programming 1 September 30th 04 09:59 PM


All times are GMT +1. The time now is 04:57 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"