0

在我已经从闪存驱动器中的文件夹中提取所有文件并将它们放在计算机上的文件夹中之后,我试图找出 Visual Basic 上的代码。我怎样才能让这个程序删除计算机上文件夹中以前日期未修改的所有文件?

这是我到目前为止所拥有的:

Imports System.IO

Public Class frmExtractionator

    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 currentFile As String In txtFiles
                Dim fileName = currentFile.Substring(sourceDirectory.Length + 1)
                File.Move(currentFile, Path.Combine(archiveDirectory, fileName))
            Next
        Catch eT As Exception
            Console.WriteLine(eT.Message)
        End Try

    End Sub
End Class
4

1 回答 1

0

Something like this will delete files that have not been modified since the given date.

Private Sub DeleteUnmodifiedFiles(directoryName As String, modificationThreshold As Date)
    Dim folder As New DirectoryInfo(directoryName)
    Dim wasModifiedSinceThreshold As Boolean
    For Each file As FileInfo In folder.GetFiles
        wasModifiedSinceThreshold = (file.LastWriteTime > modificationThreshold)
        If (Not wasModifiedSinceThreshold) Then file.Delete()
    Next
End Sub

To delete based on a number of days...

Private Sub DeleteUnmodifiedFiles(directoryName As String, modificationThresholdDays As Integer)
    Dim folder As New DirectoryInfo(directoryName)
    Dim thresholdDate As Date
    Dim wasModifiedSinceThreshold As Boolean
    For Each file As FileInfo In folder.GetFiles
        thresholdDate = DateTime.Now().AddDays(-1 * modificationThresholdDays)
        wasModifiedSinceThreshold = (file.LastWriteTime > thresholdDate)
        If (Not wasModifiedSinceThreshold) Then file.Delete()
    Next
End Sub
于 2013-04-04T15:45:11.233 回答