简体   繁体   English

按日期修改在目录和子目录中复制文件的最快方法

[英]Fastest way to copy files in directories and sub-directories by datemodified

I would like to know how do I copy the files in a directory which contains sub directories and in them it contains some files with date modified. 我想知道我怎么复制文件中包含子目录的目录,并在其中包含与修改日期的一些文件。

Now I need to copy all those files to another directory. 现在,我需要将所有这些文件复制到另一个目录。

This is what I have which copies all the files and folders but I want only the files in folders which are modified from two days before todays date. 这就是我复制所有文件和文件夹的内容,但是我只希望从今天前两天开始修改的文件夹中的文件。 (For ex: today date is 26-02-2014. I need to copy all the files from 24-02-2014 and below that date to another folder) (例如,今天是2014年2月26日。我需要将2014年2月24日及之后的所有文件复制到另一个文件夹中)

  Public Sub CopyDirectory(ByVal sourcePath As String, ByVal destinationPath As String)
    Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)

    ' If the destination folder don't exist then create it
    If Not System.IO.Directory.Exists(destinationPath) Then
        System.IO.Directory.CreateDirectory(destinationPath)
    End If

    Dim fileSystemInfo As System.IO.FileSystemInfo
    For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
        Dim destinationFileName As String =
            System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)

        ' Now check whether its a file or a folder and take action accordingly
        If TypeOf fileSystemInfo Is System.IO.FileInfo Then
            System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
        Else
            ' Recursively call the mothod to copy all the neste folders
            CopyDirectory(fileSystemInfo.FullName, destinationFileName)
        End If
    Next
End Sub

And this is the code which copies only files in a folder and dosent add any new folder which is in source path: 这是仅复制文件夹中文件的代码,然后在源路径中添加任何新文件夹:

Dim Source As New DirectoryInfo("C:\Users\username\Desktop\123")
    Dim Target As New DirectoryInfo("C:\Users\username\Desktop\345")

    Dim Files As FileInfo() =
        (From [File] As FileInfo In Source.GetFiles("*", SearchOption.AllDirectories)
         Where [File].LastWriteTime.Date.Equals(Date.Today.AddDays(-2))).ToArray

    For Each [File] As FileInfo In Files
        IO.File.Copy([File].FullName, 
                     Path.Combine(Target.FullName, [File].Name), True)
    Next [File]

So can anyone say how do I acheive this? 那么谁能说我如何做到这一点呢?

In c#, the File object has methods to determine modification date. 在c#中,File对象具有确定修改日期的方法。 So, you could do something like this: 因此,您可以执行以下操作:

DateTime today = DateTime.Today;
foreach (string filename in Directory.GetFiles("C:\\Users\\username\\Desktop\\123")) {
    DateTime fileModDate = File.GetLastWriteTime(filename);
    if (fileModDate < today.AddDays(-2)) {
        File.Copy(filename, destination);
    }
}

Here is a link that answers your query - Copy the entire contents of a directory in C# 这是回答您的查询的链接- 在C#中复制目录的全部内容

In your case you would have to add a condition before doing a File Copy. 对于您的情况,您必须在执行文件复制之前添加条件。 Something like the one shown below or the one used by John in his answer 类似于下面显示的内容或约翰在回答中使用的内容

    if (File.GetLastWriteTime(path).CompareTo(DateTime.Today) <= -2)
    {
    ...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM