Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy all files in directory

Tags:

c#

How can I copy all of the contents in one directory to another with out looping over each file?

like image 483
Kuttan Sujith Avatar asked Aug 22 '11 10:08

Kuttan Sujith


People also ask

How can I copy all files in a folder?

Copy and paste filesRight-click and pick Copy, or press Ctrl + C . Navigate to another folder, where you want to put the copy of the file. Click the menu button and pick Paste to finish copying the file, or press Ctrl + V . There will now be a copy of the file in the original folder and the other folder.

How copy all files in Linux?

Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option. The command above creates the destination directory and recursively copy all files and subdirectories from the source to the destination directory.

How do you copy all files once?

Step 1: Click on the first file to be selected. Step 2: Hold down Ctrl and click on all the files that you want to select additionally. Step 2: Press the Shift key and click on the last file. Step 3: You have now selected all files at once and can copy and move them.

How do I copy an entire directory in Linux?

In order to copy a directory on Linux, you have to execute the “cp” command with the “-R” option for recursive and specify the source and destination directories to be copied.


1 Answers

You can't. Neither Directory nor DirectoryInfo provide a Copy method. You need to implement this yourself.

void Copy(string sourceDir, string targetDir) {     Directory.CreateDirectory(targetDir);      foreach(var file in Directory.GetFiles(sourceDir))         File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));      foreach(var directory in Directory.GetDirectories(sourceDir))         Copy(directory, Path.Combine(targetDir, Path.GetFileName(directory))); } 

Please read the comments to be aware of some problems with this simplistic approach.

like image 193
Daniel Hilgarth Avatar answered Oct 13 '22 01:10

Daniel Hilgarth