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

时间:2015-06-03 13:00:14

标签: vb.net io

我想创建一个函数,该函数应该删除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

1 个答案:

答案 0 :(得分:1)

遍历目录,获取每个文件夹的属性,并获得从今天到文件夹创建日期的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