Question Delete files,folders and subfolders after 2 days

Rainny

Member
Joined
Jun 9, 2008
Messages
16
Programming Experience
Beginner
Dim oFs
Dim oFolder
Dim oFile
Dim Folder
Dim FolderSpec
Dim FileCollection

FolderSpec = "C:\testing"
Set oFs = CreateObject("Scripting.FileSystemObject")

If oFs.FolderExists(FolderSpec) Then
Set oFolder = oFs.GetFolder(FolderSpec)
Set FileCollection = oFolder.SubFolders
On Error Resume Next
For Each subfolders In FileCollection
For Each oFile In subfolders.Files
If DateDiff("d", FileDateTime(oFile), Now) >= 2 Then
oFile.Delete
End If
Next
Next
End If


The above coding is delete all the files, including the new created file.
Can anybody teach me how to delete the files, and subfolders which are more than 2 days only?
 
I would look at getting a FileInfo for each file in the directory you're monitoring.

VB.NET:
Dim di As New DirectoryInfo("C:\")
Dim fi As FileInfo() = di.GetFiles()

From there you can loop through fi and check if the timespan between today's date and file's creation date(?) is greater than 2

VB.NET:
Dim span As System.TimeSpan
...
span = Date.Now - fi(i).CreationTime
If span.Days > 2 Then
    ...
End If
 
Thank you, but i copy back the coding, just modified the little part that you provided, it also remain the same delete all the files that are newly created. Besides,, i want to ask how to delete the files in subfolders? If the subfolders in empty, how i gonna to delete the subfolder also.

Thanks a lot for all helping.
 
The logic of the code is sound; it will delete only files that were created more than 2 days ago.. With the exception of a small bug:
MattP should have suggested you use .TotalDays because using .Days will mean that a file who is 2 days, 12 hours old IS technically older than 2 days, but the .Days property will return 2.
.TotalDays will return 2.5 (2 days 12h)

Note that if you copy a file, the copy takes on the create date of the master, it is not a newly created file. Have you even looked to see whether you have any files that were created in the last 2 days?
 
Thank you, but i copy back the coding, just modified the little part that you provided, it also remain the same delete all the files that are newly created. Besides,, i want to ask how to delete the files in subfolders? If the subfolders in empty, how i gonna to delete the subfolder also.

Thanks a lot for all helping.
youre going to have to write a recursive method that takes a DirectoryInfo, lists its directries first, then its files. for each DirectoryInfo listed first, it should call itself. At the end of listing all the files, it can requery whether there are any children in the directory, then call delete.

e.g.
VB.NET:
Sub DirectoryDelete(inside as DirectoryInfo)

  For Each childDir as DirectoryInfo in inside.GetDirectories()
    DirectoryDelete(childDir)
  Next

  For Each childFile as FileInfo in inside.GetFileSystemInfos()
    if (Now - childFile.CreationTime).TotalDays > 2 Then childFile.Delete
  Next

  inside.Delete
Ens Sub

I dont present that as working code, because i'm not here to do your homework for you

Actually you may just be able to delete the parent straight out (and catch any exceptions) - dirs that are not empty might not delete

but do verify this point first
 
Thanks, but if I want to specified one directory and scan through the directory check to see while file is older than 2 days, then how should i write the coding? After check the files in the particular subfolder, if all the files in subfolders is more than 2 days, then how should i write the coding to delete the subfolder also.

Really thanks a lot for helping.
 
Try taking what I've written, reading and understanding it, then pulling it apart to a more simpler version that will do what you want. I'm not here to do your homework for you
 
I try it many time and the coding also will delete the newly created file, it doesn't care what is the date of file. when I execute the program, it delete all the files only. Besides, it doesn't delete the files inside the subfolders. The files in subfolders remain there, still cannot be deleted. How to solve the problem?

Please help me, i really need it urgently..
Thank you if you all willing to help me.
I am still a newbie in programming.
Thank you.
 
unable to delete the files

Hi, everyone, I need some help from you all.
If you all willing to help, I really appreciate it.

I try many time, but still unable to delete the files, dunno what wrong with the coding, please help me.

VB.NET:
 Sub CheckFiles(ByVal path As String)

        Dim files As String()
        Dim file As String
       Dim dt As DateTime = Directory.GetCreationTime(Environment.CurrentDirectory)

        ' Get all files from root directory
        files = dr.GetFiles(path, "*")

        For Each file In files
            If (DateTime.Now.Subtract(dt).TotalDays > 2) Then 
                 file.Delete()
                Console.WriteLine("The file was deleted")
            End If
            
        Next

    End Sub
 
As was previously posted, you need to use DirectoryInfo and FileInfo to get the information needed. The answer has been posted in this thread.

VB.NET:
    Private Sub CheckFiles(ByVal path As String)
        Dim di As New System.IO.DirectoryInfo(path)
        For Each childDir As System.IO.DirectoryInfo In di.GetDirectories
            CheckFiles(childDir.FullName)
        Next

        For Each file As System.IO.FileInfo In di.GetFileSystemInfos
            If Now.Subtract(file.CreationTime).TotalDays Then
                file.Delete()
            End If
        Next
    End Sub
 
Back
Top