在 vb 中删除超过 10 天的文件夹的功能

function to delete folders older than 10 days in vb

我想创建一个函数,该函数应删除一个文件夹中 10 天前的所有子文件夹。

Shell script to delete directories older than n days

我想显示所有文件夹并计算旧文件夹的数量,如果超过 10 天则删除。

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

这部分不工作

  If days > 10 Then
   fi.Delete()
  End If

遍历目录,获取每个文件夹的属性,并获取从今天到文件夹创建日期的 TimeSpan 差异。

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