Multiple File Deletions Based on Creation Dates

C

crjunk

I have a folder named TXT24 on my web server that will contain multiple
txt files. I want to add some code to the Session_Start event in my
Global.aspx page that will scan all the files in the TXT24 folder and
delete any text file that is over 24 hours old based on file's creation
date. All the files in this folder will have random names, so I can't
specify the file names in my code that need to be deleted.
Can anyone give me some leads on how to do this?

Thanks,
crjunk
 
P

Peter Rilling

crjunk said:
I have a folder named TXT24 on my web server that will contain multiple
txt files. I want to add some code to the Session_Start event in my
Global.aspx page that will scan all the files in the TXT24 folder and
delete any text file that is over 24 hours old based on file's creation
date. All the files in this folder will have random names, so I can't
specify the file names in my code that need to be deleted.
Can anyone give me some leads on how to do this?

Thanks,
crjunk
 
P

Peter Rilling

1) Instantiate a DirectoryInfo object for your temp folder.
2) Use DirectoryInfo.GetFiles to get a reference to all the files in the
folder.
3) Cycle through each file and use the FileInfo.CreationTime property to
determine if it should be removed.
4) Use the FileInfo.Delete to remove the file.

Make sure IIS has access to that folder.
 
C

crjunk

Thanks for your help. I added a couple of excel files to the directory
just for grins and I'm under the assumption that only text type files
can be deleted, binary type files cannot be deleted. Here is what I
came up with:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)Handles Button1.Click

Dim tmpFDir As System.IO.DirectoryInfo
Dim tmpFArray As System.IO.FileInfo()
Dim tDateTime As Date
Dim i As Integer

tmpFDir = New System.IO.DirectoryInfo("C:\TXT24")
tmpFArray = tmpFDir.GetFiles()

For i = 0 To tmpFArray.Length - 1
tDateTime = DateAdd(DateInterval.Hour, 24,
tmpFArray(i).CreationTime)
If tDateTime < Now() Then
tmpFArray(i).Delete()
End If
Next

End Sub


Thanks again!
crjunk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top