0

如您所见,以下代码将从一个文件夹中提取文件并将它们放置在另一个文件夹中。但是我正在尝试删除过去3个月内没有修改过的文件,它根本不会删除任何文件。

代码:

Imports System.IO

Public Class frmExtractionator

    Dim txtFiles1 As Control

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

        Dim sourceDirectory As String = "E:\CopierFolderforTestDriveCapstone"
        Dim archiveDirectory As String = "E:\FilesExtracted"

        Try
            Dim txtFiles = Directory.EnumerateFiles(sourceDirectory)

            If (Not System.IO.Directory.Exists(archiveDirectory)) Then
                System.IO.Directory.CreateDirectory(archiveDirectory)
            End If

            For Each currentFileLoc As String In txtFiles
                Dim fileName = currentFileLoc.Substring(sourceDirectory.Length + 1)
                File.Move(currentFileLoc, Path.Combine(archiveDirectory, fileName))

                Try
                    Dim di As DirectoryInfo = New DirectoryInfo(sourceDirectory)
                    Dim fi As FileInfo() = di.GetFiles()

                    For Each currentFile As FileInfo In fi
                        File.Move(currentFile.FullName, Path.Combine(archiveDirectory, currentFile.Name))

                        Dim dt As DateTime = currentFile.LastWriteTime
                        ' Add 3 months to the last write and check if it is less than today '
                        If dt.AddMonths(3) < DateTime.Today Then
                            File.Delete(currentFile.FullName)

                        End If
                    Next
                Catch eT As Exception
                    Console.WriteLine(eT.Message)
                End Try

            Next
        Catch eT As Exception
            Console.WriteLine(eT.Message)
        End Try
    End Sub
End Class
4

0 回答 0