How do I copy all files from one directory to another directory and overwrite all existing same-named files in the target directory with C#?
I have the following code to copy the files from one directory to another directory...
const string sourceDir = @"C:\AppProject\Smart\SmartStaff\site\document";
const string targetDir = @"C:\AppProject\Smart\ExternalSmartStaff\site\document";
foreach (var file in Directory.GetFiles(sourceDir))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));
..., but when the target directory already contains a file with the same name as a file in the source directory, it fails with the error System.IO.IOException: The file 'C:\AppProject\Smart\ExternalSmartStaff\site\document\SomeDocument.txt' already exists.
.
To be clear, given the following directories and files in them...
C:\>dir C:\AppProject\Smart\SmartStaff\site\document
...
Directory of C:\AppProject\Smart\SmartStaff\site\document
09/03/2014 06:38 PM <DIR> .
09/03/2014 06:38 PM <DIR> ..
05/25/2014 08:29 PM 44 SomeDocument.txt
05/25/2014 08:32 PM 21 SomeDocument2.txt
05/25/2014 08:36 PM 21 SomeDocument3.txt
05/25/2014 08:43 PM 44 SomeDocument4.txt
4 File(s) 130 bytes
2 Dir(s) 128,326,766,592 bytes free
C:\>dir C:\AppProject\Smart\ExternalSmartStaff\site\document
...
Directory of C:\AppProject\Smart\ExternalSmartStaff\site\document
09/03/2014 06:39 PM <DIR> .
09/03/2014 06:39 PM <DIR> ..
09/03/2014 06:39 PM 26 SomeDocument.txt
09/03/2014 06:39 PM 54 SomeDocument2.txt
2 File(s) 80 bytes
2 Dir(s) 128,326,766,592 bytes free
..., I would like C:\AppProject\Smart\ExternalSmartStaff\site\document
to look like this after the file copy in C#:
C:\>dir C:\AppProject\Smart\ExternalSmartStaff\site\document
...
Directory of C:\AppProject\Smart\ExternalSmartStaff\site\document
09/03/2014 06:47 PM <DIR> .
09/03/2014 06:47 PM <DIR> ..
05/25/2014 08:29 PM 44 SomeDocument.txt
05/25/2014 08:32 PM 21 SomeDocument2.txt
05/25/2014 08:36 PM 21 SomeDocument3.txt
05/25/2014 08:43 PM 44 SomeDocument4.txt
4 File(s) 130 bytes
2 Dir(s) 128,327,835,648 bytes free
How can I avoid the IOException
and accomplish this?
Switches are explained below. /XC − Prevents overwriting the files which have the same timestamp. /XN − Prevents overwriting of files with the newer timestamp than the source files. /XO − Prevents overwriting of files with the older timestamp than the source files.
Save As -> Replace File Select "Replace File. This is the same behavior as File Save." The original file will be overwritten.
By default, existing files are overwritten. To control overwrite behavior, use --overwrite. (If the files are identical no transfer occurs regardless of this setting value.) Because scp uses authentication and encryption provided by ssh, a Secure Shell server must be running on the remote computer.
By default when you run the PowerShell Copy-Item cmdlet, it will overwrite the file if it is already exists.
Try this:
const string sourceDir = @"C:\AppProject\Smart\SmartStaff\site\document";
const string targetDir = @"C:\AppProject\Smart\ExternalSmartStaff\site\document";
foreach (var file in Directory.GetFiles(sourceDir))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)), true);
Basically, you just need to call a different overload of File.Copy
. MSDN documentation explains that the third, bool
parameter is to indicate whether to overwrite existing files with the files being copied.
Rather use
File.Copy Method (String, String, Boolean)
Copies an existing file to a new file. Overwriting a file of the same name is allowed.
Where
overwriteType: System.Boolean
true if the destination file can be overwritten; otherwise, false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With