File copy using robo copy and process
Solution 1
I would just use System.IO. Should be plenty fast enough, and your filename could be a wildcard.
using System.IO;
// snip your code... providing fileName, sourceDir, destinationDir
DirectoryInfo dirInfo = new DirectoryInfo(sourceDir);
FileInfo[] fileInfos = dirInfo.GetFiles(fileName);
foreach (FileInfo file in fileInfos)
{
File.Copy(file.FullName, Path.Combine(destinationDir, file.Name), true); // overwrites existing
}
Solution 2
This question is a bit old but I thought I would answer to help anyone who still land on it. I wrote a library called RoboSharp (https://github.com/tjscience/RoboSharp) that brings all of the goodness in Robocopy to c#. Take a look if you require the power of Robocopy in c#.
Solution 3
Process p = new Process();
p.StartInfo.Arguments = string.Format("/C Robocopy /S {0} {1}", "C:\\source", "C:\\destination");
p.StartInfo.FileName = "CMD.EXE";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
/C Robocopy -> this is a command to run robocopy
/S -> This will help to copy sub folders as well as Files
Solution 4
You should call File.Copy
in a loop.
Biju Thomas
Updated on March 04, 2020Comments
-
Biju Thomas about 4 years
I am creating a File copy program which will copy large number of files(~100,000) with size ~50 KB using ROBOCOPY command.
For each file, I am creating a new process and passing the ROBOCOPY command and arguments as follow:
using (Process p = new Process) { p.StartInfo.Arguments = string.Format("/C ROBOCOPY {0} {1} {2}", sourceDir, destinationDir, fileName); p.StartInfo.FileName = "CMD.EXE"; p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; p.Start(); p.WaitForExit(); }
Instead of creating a process for each file, I am looking for a better approach, which will be good in terms of performance and design. Can someone suggest a better method?
-
Marco over 12 yearsAnd if he doesn't want to block main thread, he could do this job in a separate thread (maybe a
BackgroundWorker
) which notifies UI... -
Biju Thomas over 12 yearsFile.Copy is really slow when compared to ROBOCOPY. Right?
-
William Stearns over 12 yearsProbably not considering how the original question is posed. Shelling out to a process and waiting for it is probably slower overall. Considering some of the other comments, may want to still consider doing the File.Copy, but be creative in how you fire off the copy process. Possibly consider using a separate thread to do the copying unattended.
-
Tobias Kienzler over 10 years@user2045570 Why didn't you remove the empty lines while you're at it?
-
nedaRM over 10 years@TobiasKienzler must have missed it. Will watch out for empty lines next time.
-
Honorificabilitudinitas over 8 yearsYour answer is not useful since File.copy does not have all features of robocopy. for eg: Longpath support
-
NielW over 6 yearsAwesome! Does everything I've been looking for!
-
Igor Meszaros about 5 yearsThis wasn' t the question
-
Andy over 2 yearsThis would block the main thread when u try to copy files through network
-
Datboydozy almost 2 yearsHi I see this and it looks good but do you have a proper documentation on how to run this package? I referenced your wiki but it has a very short code snippet and for an inexperienced coder like myself, I have no idea why or how I would use void backup_OnFileProcessed(object sender, FileProcessedEventArgs e) to copy files. The only way to use your library is contained here: github.com/tjscience/RoboSharp/wiki/Code-Snippets-(C%23) And there just isn't enough info on how to use it from start to finish. Or a description of what's going on on each step @xcopy