Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Visual Basic Program/Script to delete old files w/in a folder

Status
Not open for further replies.

dpu

IS-IT--Management
Jan 24, 2005
179
US
I am in need of a program or script that will allow me to delete files within a folder after a certain date. Can anyone direct me in how to do this. I am fairly new to VB.
 
Hi dpu

This may not be exactly what you are looking for but you can follow the same idea to accomplish your task.
FYI this is done in VB.Net.....

Dim dInfo As DirectoryInfo = New DirectoryInfo("The path to your folder")

Dim NumOfDays as Integer = Some Integer

'Function that deletes files from the folder after X amount of days
Public Sub deleteAfterDays(ByVal d As DirectoryInfo, ByVal days As Integer)
Dim ts As TimeSpan
For Each f As FileInfo In d.GetFiles
ts.Subtract(f.CreationTime - Date.Now)
If ts.TotalDays > days Then
f.Delete()
End If
Next
End Sub

When you call the function, you pass in the two variables as parameters. ex: deleteAfterDays(dInfo, NumOfDays)

Note: You may not want to delete files that you modify frequently. In that case replace f.CreationTime with f.LastWriteTime. f.LastWriteTime will get the date the file was last modified, while f.CreationTime will get you the date the file was created.

"Cowards die many times before their deaths, The valiant never taste of death but once." - Julius Caesar
 
Thank you. I am goint to give this a try.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top