0

如您所见,以下代码将提取我的“CopierFolderforTestDriveCapstone”文件夹中的所有文件,并将所有文件放入我的“FilesExtracted”文件夹中。我的问题是在所有文件都放入“FilesExtracted”文件夹之后,如何删除所有最近未修改的文件。我只想保留在过去几个月中修改过的文件,或者去年任何一个都可以弄清楚。谢谢 :)

导入系统.IO

公共类 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 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
4

1 回答 1

0

您可以使用DirectoryInfo类和FileInfo类来获取有关连接到文件的各种时间属性的信息

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
于 2013-04-05T15:32:28.257 回答