1

I want to create a function which should delete all subfolders of a folder which are 10 days older.

Shell script to delete directories older than n days

I want show all the folder and count how much old and delete if it is 10 days old.

enter code here
Private Function test(ByVal directory As String) As String()
Dim fi As New IO.DirectoryInfo(directory)
Dim path() As String = {}
For Each subfolder As IO.DirectoryInfo In fi.GetDirectories()
 Array.Resize(path, path.Length + 1)
 path(path.Length - 1) = subfolder.FullName
 For Each s As String In test(subfolder.FullName)
 Array.Resize(path, path.Length + 1)
 path(path.Length - 1) = s
 Dim w = IO.Path.GetFileName(s)
 '' ListBox1.Items.Add(w)
 Dim iDate As String = w
 Dim oDate As DateTime = Convert.ToDateTime(iDate)
 ''MsgBox(oDate.Day & " " & oDate.Month & "  " & oDate.Year)
 DateTimePicker1.Value = DateTime.Today
 Dim date2 As Date = oDate
 Dim span = DateTimePicker1.Value - date2
 Dim days As Double = span.TotalDays
 '' MsgBox(days)
 '' ListBox1.Items.Add(days)
    Next
   Next

this part is not working

  If days > 10 Then
   fi.Delete()
  End If
Community
  • 1
  • 1
TOM
  • 873
  • 1
  • 12
  • 35
  • Without reviewing the whole code, but why do you think that `If days < 10` deletes folders that are olders than 10 days? Should it be `> 10`? – Tim Schmelter Jun 03 '15 at 13:02
  • Apart from that(now fixed), why do you use `IO.Path.GetFileName` to get the `Date`? You could use `Directory.GetCreationTime(path)` or `DirectoryInfo.CreationTime`. – Tim Schmelter Jun 03 '15 at 13:05
  • to get the file name .which are in format of "mm/dd/yyyy" – TOM Jun 03 '15 at 13:07
  • i have find difference between today's date and File name date – TOM Jun 03 '15 at 13:10
  • Have you used the debugger? What happened, what was wrong, what value had `oDate`, what value had `DateTimePicker1.Value`? Maybe months and days were swapped, like `02/03/2015` which could mean march in germany but february in USA. Note that `/` will be replaced by the actual localized date separators of your current culture. – Tim Schmelter Jun 03 '15 at 13:11
  • that folder are created by compuer/mechine – TOM Jun 03 '15 at 13:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79540/discussion-between-tom-and-tim-schmelter). – TOM Jun 03 '15 at 13:14

1 Answers1

1

Iterate through the directory, get each folder's properties, and get the TimeSpan difference from today to the folder's creation date.

Try
    Dim dtCreated As DateTime
    Dim dtToday As DateTime = Today.Date
    Dim diObj As DirectoryInfo
    Dim ts As TimeSpan
    Dim lstDirsToDelete As New List(Of String)

    For Each sSubDir As String In Directory.GetDirectories(sDirectory)
        diObj = New DirectoryInfo(sSubDir)
        dtCreated = diObj.CreationTime

        ts = dtToday - dtCreated

        'Add whatever storing you want here for all folders...

        If ts.Days > 10 Then
            lstDirsToDelete.Add(sSubDir)
            'Store whatever values you want here... like how old the folder is
            diObj.Delete(True) 'True for recursive deleting
        End If
    Next
Catch ex As Exception
    MessageBox.Show(ex.Message, "Error Deleting Folder", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Keith
  • 1,331
  • 2
  • 13
  • 18
  • 1
    You may consider using `LastWriteTimeUtc`. UTC will eliminate issues with timezone and daylight savings settings, while unlike `CreationTime`,`LastWrite` will change with edits thus providing a more accurate date. – JStevens Jun 03 '15 at 15:03