File copy using robo copy and process

a 夏天 提交于 2019-12-08 15:46:35

问题


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?


回答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
}



回答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#.




回答3:


You should call File.Copy in a loop.




回答4:


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



回答5:


.cmd has 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 starts with 4 multiple windows copying files simultaneously as per above commands, it waits for another file, as it has wait time, if file is being used by another process. Is is more faster as it do job simultaneously.

Now I am developing GUI using C# windows to run the process instead going to command console and
start

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

However, if it takes control of one process, i.e. cmd.exe and and there are 4 robocopy processes in taskmanager. when cmd.exe process completes, it returns the cursor to label.text "Sucesssfully completed". While there are robocopy processes still running. you can see the robocopy windows doing the copying process.

Here is the question: I want to take control of all the processes (cmd.exe and robocopy.exe) programatically in C#, so that when the label.text should display "successfully completed" only when all commands are successfully completed", if one fails, then there is no point in the GUI.

option 2 (similar to Biju has written above): is it better to remove robocopy command scripts from sample.cmd(batch file) file and write code to run the 4 robocopy lines in C#, but how to run the robocooy script line written .cmd file, as they have arguments as well. I code runs each robocopy process then each will return to the next line of code and if it fails, we can catch the error and display in the message box.

Hope this will help... However, I am looking for more better way, if somebody can improve on the same.



来源:https://stackoverflow.com/questions/7890767/file-copy-using-robo-copy-and-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!