0

I am a bit confused about File.Copy. Initially, I was deleting a whole directory structure and then copying from a source path to a target path, but this was taking a while. Now what I am doing is only creating the directory structure on the target path if it does not already exist. If it exists, I only want to copy files if they are newer. After removing the delete, the copy goes super fast, but I am not sure if it is actually copying newer files. If I do File.Copy(source,target), does this only copy files if the do not exist? If I do File.Copy(source,target,true), does this copy the file regardless if it is newer or not?

Xaisoft
  • 45,655
  • 87
  • 279
  • 432

3 Answers3

1

File.Copy(source,target,true) will overwrite the file - regardless if it is newer or not.

Copy doesn't have logic to determine newness of files or what would be the right action.

You need to implement this logic yourself - if you only want to copy newer files, you need to compare the create dates of both files and only copy newer ones.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • If that is the case, was it the delete that was making it take so long and if it copies it regardless, why does it go so fast, it literally takes under 1 second where before it took over 30 seconds. – Xaisoft Nov 01 '12 at 15:08
  • Compare the create date on the source with the create date on the target? – Xaisoft Nov 01 '12 at 15:09
  • @Xaisoft - The amount of time could be due to any number of reasons. I will not speculate. And yes, the create dates of the source and target. – Oded Nov 01 '12 at 15:10
  • Does it matter if the source is on one server and the target is your local machine? Would that throw the Create time off? – Xaisoft Nov 01 '12 at 15:18
  • @Xaisoft - It might, if the clocks on the different systems are not synchronized. Checking against UTC on both sides _should_ help, but you will need to test this. – Oded Nov 01 '12 at 15:30
  • Is it better to check if the create time on the source > create time on target or if the modified time on source != modified time on target? – Xaisoft Nov 01 '12 at 15:41
1

You should use the FileInfo Class and compare the files in your logic.

Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188
1

If I do File.Copy(source,target), does this only copy files if the do not exist?

File.Copy(source, target) will throw an IOException if the target file already exists (regardless of whether it's newer or not).

If your code "runs fast" using this, I assume that you are "swallowing exceptions" somewhere (i.e. have a try with an empty catch block). That's evil, because it makes your program "appear" like it is working correctly when it isn't. Don't do that! It makes debugging a nightmare.

If I do File.Copy(source,target,true), does this copy the file regardless if it is newer or not?

Yes.

If you want the files to be copied based on some attribute, you can use the File or the FileInfo class (What's the difference?) to get this information. You can choose between the "Creation Time", the "Last Access Time" and the "Last Write Time" (depending on how you define "newer").

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • I am not swallowing exceptions because I check if the File exists before trying to copy, but I guess I need to add to this logic to teest for one of the attributes (CreateTime, LastAccessTime, etc) – Xaisoft Nov 01 '12 at 15:13
  • So I guess it is not copying anything, lol. I already implemented File.Copy, can I just use FileInfo to get Create time for example, but still use File.Copy – Xaisoft Nov 01 '12 at 15:15
  • Sure. You will just have to use the `File.Copy(_, _, true)` variant to make sure that the file is really overwritten. – Heinzi Nov 01 '12 at 15:17
  • There is a `File.GetCreationTime` and `File.GetCreationTimeUTC`. What is wrong with this as opposed to the ones found in FileInfo? – Xaisoft Nov 01 '12 at 15:17
  • @Xaisoft: Nothing, both are fine. See http://stackoverflow.com/q/1324788/87698 for a comparison. Thanks for raising this point, I've added it to my answer. – Heinzi Nov 01 '12 at 15:18
  • If I check CreationTime, if the source is on one server and the target is my local machine, does this throw things off? Would there be an alternative attribute in this case? – Xaisoft Nov 01 '12 at 15:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18924/discussion-between-heinzi-and-xaisoft) – Heinzi Nov 01 '12 at 15:21