Copying a file using a robo copy and process

I am creating a file copy program that copies a large number of files (~ 100,000) with a size of ~ 50 KB using the ROBOCOPY command.

For each file, I create a new process and pass the ROBOCOPY command and arguments as follows:

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 that will be good in terms of performance and design. Can anyone suggest a better method?

+8
c # windows file copy robocopy
source share
5 answers

I would just use System.IO. It should be fast enough, and your file name can 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 } 
+3
source share

This question is a little old, but I thought I would answer to help anyone who still lands on it. I wrote the RoboSharp library ( https://github.com/tjscience/RoboSharp ) which brings all the goodness of Robocopy to C #. Take a look if you need the power of Robocopy in C #.

+9
source share

You should call File.Copy in a loop.

+2
source share

.cmd has the following lines

 Start ROBOCOY src dest a* b* c* /z /w:1 r:1 Start ROBOCOY src dest d* e* f* g* /z /w:1 r:1 Start ROBOCOY src dest h* K* P* Y* /z /w:1 r:1 Start ROBOCOY src dest xry* srp* /z /w:1 r:1 

When I run> Robocopy sample.cmd I start by copying the files 4 times at the same time according to the commands above, it waits for another file, since it has a timeout if the file is used by another process. More faster as it does the job at the same time.

Now I am developing a GUI using C # windows to start the process and then for the management console and
start

  main() { process.start( "path of sample.cmd" ) process.waitforexit() label.text=" sucessful copy" } 

However, if it takes control of one process, that is cmd.exe, and there are 4 robocopy processes in the task manager. when the cmd.exe process terminates, it returns the cursor to label.text "Sucesssfully completed". While robocopy processes are still in progress, you can see robocopy windows performing the copy process.

Here's the question: I want to take control of all processes (cmd.exe and robocopy.exe) programmatically in C #, so that when label.text should only display “successfully completed” when all commands completed successfully, “if one fails, then the graphical interface makes no sense.

option 2 (similar to Biju, written above): is it better to delete scripts of robocopy commands from sample.cmd (batch file) and write code to run 4 lines of robocopy in C #, but how to run robocooy script a written .cmd file, since they have there are arguments. Since the code starts each robocopy process, each of them will return to the next line of code, and if this fails, we can catch the error and appear in the message box.

Hope this helps ... However, I am looking for a better way if someone can improve the same.

0
source share
 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 
0
source share

All Articles