Visual Basic - 将最近修改的文件复制到另一个文件夹

时间:2013-04-11 14:57:01

标签: visual-studio-2010

我听到的以下代码将提取某个文件夹中的所有文件,然后复制所有这些文件并将它们放入另一个文件夹中。我今天的问题是如何修改此代码,以便最近只修改了从原始文件夹中提取的文件。即使您可以告诉我如何仅提取从今天修改过的文件也会很棒。感谢所有帮助过的人!

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
        'DeleteUnmodifiedFiles(sourceDirectory, -14)

        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))
        Next
    Catch eT As Exception
        Console.WriteLine(eT.Message)
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用Directory.GetLastWriteTime方法确定上次写入文件的时间。

来自链接:

  

返回上次指定文件或目录的日期和时间。

Dim checkDate As Date = Date.Parse("01/1/2013")

For Each currentFileLoc As String In txtFiles
    Dim fileName = currentFileLoc.Substring(sourceDirectory.Length + 1)
    If Directory.GetLastWriteTime(Path.Combine(sourceDirectory, fileName)) > checkDate Then
        File.Move(currentFileLoc, Path.Combine(archiveDirectory, fileName))
    End If
Next