Copy all files from one directory to another c#


















CreateDirectory folders. Target ; foreach var file in Directory. GetFiles folders. Copy file, Path. Combine folders. Target, Path.

GetDirectories folders. Push new Folders folder, Path. Jens Granlund Jens Granlund 4, 1 1 gold badge 32 32 silver badges 30 30 bronze badges. Hard to imagine blowing the stack before glowing the path limit — Ed S.

Here's a utility class I've used for IO tasks like this. Zero; FileOpStruct. Ben Ben. AllDirectories Directory. CreateDirectory Path. Combine DestinationPath ,dirPath. Remove 0, SourcePath. AllDirectories File. Copy newPath, Path. Combine DestinationPath , newPath. In my case, for the directories, I had to use Path. Join instead of Path. I don't fully understand why though, but I guess I was doing something related to this remark in the documentation , which recommends Path.

Join — davrob ForEach Directory. Escape path , newPath ; if File. GetDirectoryName output ; File. Exists return; if! Exists target. Create ; Parallel. ForEach source. FullName, sourceChildDirectory. Name ; Parallel. FullName, sourceFile. Jerry Liang Jerry Liang 9 9 silver badges 27 27 bronze badges. Delete zip ; Note: ZipFile is available on. NET 4. Compression namespace. Ohad Schneider AlexanderD AlexanderD 9 9 silver badges 18 18 bronze badges.

Neither do I, hence the question, but the selected answer doesn't need recursion. This answer creates a zip file on disk, which is a lot of additional work for a file copy - not only are you creating an additional copy of the data, but you're spending processor time compressing and decompressing it. I'm sure it works, the same way you can probably knock a nail in with your shoe, but it's more work with more things that can go wrong, while there are better ways of doing it.

The reason I ended up with this is string replacements. As others have pointed out, the accepted answer presents many concerns; junction link may not work, as well as repeating folder pattern or files without extension or name. Less code, less chance to go wrong. And since processor time is not a concern for me, it makes it suitable for my specific case — AlexanderD.

Yeah, that's like driving miles out of your way to avoid a single traffic light, but it's your journey, so go for it. Checking for folder patterns is trivial compared to what ZIP needs to do under the hood. I'd strongly recommend against this for anyone who cares about not wasting processor, disk, electricity or where this needs to run alongside other programs on the same machine. Also, if you're ever asked this type of question at interview never go with "my code is simple so I don't care about processor time" - you won't get the job.

I switched to the answer provided by justin-r. Still, I'll leave this answer there as just another way of doing it — AlexanderD. If the folders are on separate network shares and contain a lot of files, this would be the best option in my opinion. Start info ; process. ReadToEnd ; if process. Chris S Chris S Combine destination, Path. GetFileName source ; File. Combine destination, fileName ; if!

See the selected answer, by using the SearchOption flag on the searches for folders and files it does this in 4 lines of code. Also check out the. HasFlag extension now on enums. CreateSubdirectory source. Name ; foreach var fileInfo in source. GetFiles fileInfo. Combine newDirectoryInfo. FullName, fileInfo. Name ; foreach var childDirectoryInfo in source. DirectoryCopy ". Combine destDirName, file.

Name ; file. Combine destDirName, subdir. Name ; DirectoryCopy subdir. This is great -- Keep in mind the line file. CopyTo temppath, false ; says "copy this file to this place, only if it doesn't exist", which most of the time isn't what we want. But, i can understand why it defaults to that. Maybe add a flag to the method for overwriting files. Here is a concise and efficient solution: namespace System. CreateDirectory destPath ; Parallel. ForEach srcPath. Copy srcInfo.

CopyTo destinationPath ;. WriteLine ex. CreateDirectory dirs[j]. Copy files[j], files[j]. Create ; foreach var sourceSubDirPath in Directory. CreateDirectory sourceSubDirPath. Replace sourcePath, destinationPath ; foreach var file in Directory. Copy file, file.

Daryl Daryl 3, 4 4 gold badges 26 26 silver badges 37 37 bronze badges. Use this class. Name ; foreach var sourceFile in source. GetFiles sourceFile. Ahmed Sabry Ahmed Sabry 3 3 silver badges 8 8 bronze badges. This is similar to other answers, refactored to use. ForEach which is slightly more work, memory and slightly slower than just enumerating the directories directly and as an extension method. The selected answer uses SearchOption.

AllDirectories and avoids recursion, so I'd recommend switching to that model. Also, you usually don't need the name of the type in extension methods - I'd rename it to CopyTo so that it became sourceDir. CopyTo destination ; — Keith. One variant with only one loop for copying of all folders and files: foreach var f in Directory. Exists f File. Copy f, output, true ; else Directory. Termininja Termininja 6, 12 12 gold badges 43 43 silver badges 46 46 bronze badges.

If you're going to use Regex , you should probably also Regex. Escape path as part of your expression composition especially considering the Windows path separator. You might also get benefit from creating and maybe compiling a new Regex object outside of the loop, rather than relying on the static method.

GetDirectories source. Replace source. FullName, destination ; Directory. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid. Related Questions. Script to copy folders from one network location to another.

Github - copying from one branch to another. Directory Copy from One System to Another. How To copy a database to another location. Copy database to another location. Copying a Complte Directory to Another System. How to copy files from one location to another location throgh sql. Sample projects on Copying large files from one location to another using windows service. Layout: fixed fluid. Web03 2. Strip HTML. Encode HTML. Paste as-is. Code block.

Quoted Text. FileSystemWatcher to provide events that will enable you to calculate the progress when operating on multiple files. Another approach is to use platform invoke to call the relevant file-related methods in the Windows Shell.

Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Is this page helpful? Please rate your experience Yes No.



0コメント

  • 1000 / 1000