简体   繁体   English

仅复制较新的文件

[英]Copy only files that are newer

I am currently using this code: 我目前正在使用此代码:

    if (!Directory.Exists(command2)) Directory.CreateDirectory(command2);

    if (Directory.Exists(vmdaydir)) Directory.Delete(vmdaydir,true);

    if (!Directory.Exists(vmdaydir)) Directory.CreateDirectory(vmdaydir);

    var dir = Path.GetDirectoryName(args[0]);

    sb.AppendLine("Backing Up VM: " + DateTime.Now.ToString(CultureInfo.InvariantCulture));
    Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(dir, vmdaydir);
    sb.AppendLine("VM Backed Up: " + DateTime.Now.ToString(CultureInfo.InvariantCulture));

As you can see, I am deleting the directory, then I am copying the folder back. 如您所见,我先删除目录,然后再将文件夹复制回去。 This is taking way to long since the directory is ~80gb in size. 由于目录的大小约为80gb,因此需要很长时间。 I realized that I do not need to copy all the files, only the ones that have changed. 我意识到我不需要复制所有文件,只需复制已更改的文件。

How would I copy the files from one folder to another but only copying the files that are newer? 如何将文件从一个文件夹复制到另一个文件夹,而仅复制较新的文件? Anyone have any suggestions? 有人有什么建议吗?

==== edit ==== ====编辑====

I assume I can just do a file compare of each file and then copy it to the new directory, iterating through each folder/file? 我假设我可以对每个文件进行文件比较,然后将其复制到新目录中,遍历每个文件夹/文件? Is there a simpler way to do this? 有没有更简单的方法可以做到这一点?

Use the FileInfo class, and use the LastWriteTime property to get the last modified time of the file. 使用FileInfo类,并使用LastWriteTime属性获取文件的最后修改时间。 Compare it to the time you're checking against and take only files that are later. 将其与您要检查的时间进行比较,并仅获取以后的文件。

Loop through the files in the directory, checking the last modified time (FileInfo.LastWriteTime) - any files that are newer are copied over. 循环浏览目录中的文件,检查上次修改时间(FileInfo.LastWriteTime)-任何较新的文件都将被复制。

See FileInfo Class for more information. 有关更多信息,请参见FileInfo类

You need to be careful when trying to do this that you can get a lock on the file otherwise another application may not be finished with it and you may try to copy it before you are ready. 尝试执行此操作时,请务必小心,以免锁定该文件,否则可能无法完成另一个应用程序,并且在准备就绪之前可以尝试将其复制。

So follow these steps... 因此,请按照以下步骤操作...

1) attempt to lock file 1)尝试锁定文件

2) if(got lock) copy file 2)if(锁住)复制文件

3) else wait a short time 3)否则等待一会儿

4) goto 1 4)转到1

:) :)

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

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