Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I copy files, overwriting existing files? [duplicate]

Tags:

c#

.net

file-io

Overview

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..

Details

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?

like image 637
Jin Yong Avatar asked May 15 '13 04:05

Jin Yong


People also ask

How do I copy files without overwriting?

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.

How do I overwrite an existing file?

Save As -> Replace File Select "Replace File. This is the same behavior as File Save." The original file will be overwritten.

Does SCP overwrite existing files?

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.

Does copy item overwrite by default?

By default when you run the PowerShell Copy-Item cmdlet, it will overwrite the file if it is already exists.


2 Answers

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.

like image 58
J0e3gan Avatar answered Oct 13 '22 02:10

J0e3gan


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.

like image 41
Adriaan Stander Avatar answered Oct 13 '22 01:10

Adriaan Stander