Robocopy Batch File in Master Deployer


Introduction
 
Master Deployer uses a Robocopy Batch file to copy files from one server to another. Here the point to be noted is that Robocopy can copy files from one shared folder in one server to another shared folder in another server so before starting please make sure that all the folders (Source and Destination) are shared. Before using the application make sure that Robocopy.exe is present in C:>windows>System32. Robocopy is supported by Windows Vista, Windows 7, Windows Server 2003 and higher versions of Windows.
 
Using The Code
 
In this application I am using a Robocopy script to copy files from one server to another.
Again Point to be Noted Both the Source and Destination Folders Should be shared otherwise Batch script will not work.
Also Create one Text file for writting logs .
 
Script
 

 :Start
 SET robo_path=C:\Tools\Robocopy.exe //The Path of your Robocopy exe file
 SET source_dir="\\your Source Server Name\your Source Shared Folder"// The Path Of Your Source
 SET dest_dir="\\your destination server Name\your Destination Shared Folder"//The Path of your destination
 SET log_fname=c:\BackupDocsAndSettingsLog.txt //your Log file path in Source Server
 
 SET what_to_copy=/COPY:DAT //Copy Command
 
 REM Refer to the robocopy documentation for more details.
 REM /R:n :: number of Retries
 REM /W:n :: Wait time between retries
 REM /LOG :: Output log file
 REM /NFL :: No file logging
 REM /NDL :: No dir logging
 REM /E :: Include Empty Subdirectories
 REM /XA:H :: Excludes files with hidden attribute
 REM /XJ :: Exclude Junction Points (prevents recursive Application Data loop in Vista)
 SET options=/R:1 /W:1 /LOG+:%log_fname% /NFL /NDL /E /XJ
 
 REM Execute the command based on all of our parameters
 %robo_path% %source_dir% %dest_dir% %what_to_copy% %options% %exclude_dirs% %exclude_files%
 
 :END
 
C# Code
 
The following code has to be written for the button click event. I am using a process class to execute a batch file which requires the "system.Diagnostics" namespace.
 
 
private void button_server_Click(object sender, EventArgs e)
 {
 
   try
 
   {
 
       Process p = newProcess();
 
       // Redirect the output stream of the child process.
 
       p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.FileName = "c:\\Tools\\RobocopyScript_76.Bat";//Your Batch File path Give it according to where you have kept ur batch file.
        //Path of the script in source server
        p.Start();
 
       // Do not wait for the child process to exit before
        // reading to the end of its redirected stream.
        // p.WaitForExit();
        // Read the output stream first and then wait.
 
       string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit(); 
    }
 
   catch (Exception ex)
    {
        labelError.Text = ex.Message.ToString(); 
    }
 }

 
Application Front Snap

deployer.gif
Point to be Noted
 
For different servers you have to create different batch files with different destination and source addresses. And you can add different buttons and inside their event you can run the respective batch file.


Similar Articles