Question

thanks in advance for your time.

I'm working on an application to automate some of the things that I have to check each day.

One of them is making sure that all of our daily Robocopies took place successfully.

I'm having issues figuring out the best way to communicate this to my program. I've looked into the various Exit codes that Robocopy uses which would be very useful, but is there anyway to output this to a file, and would there be anyway to know which backup it was associated with?

Sorry if this is vague, please let me know of any other info that could be of use.

Thanks, Will

Was it helpful?

Solution

If you start the robocopy process from your application you could check the exit code from there.

Process myProcess = null;

// Start the process.
myProcess = Process.Start("robocopy ....");

myProcess.WaitForExit(1000);

Console.WriteLine("Process exit code: {0}",  myProcess.ExitCode);

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode(v=vs.110).aspx

OTHER TIPS

You could write the Robocopy output to a file and read that in, like

robocopy c:\Test\ c:\temp /LOG+:myLogFile

Then experiment with the log options to get a useful verbosity ( see http://ss64.com/nt/robocopy.html )

You could do something like this:

public static void Main()
{
    // Prepare the process.
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = false;
    startInfo.UseShellExecute = false;
    startInfo.FileName = "robocopy.exe";
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.Arguments = "WHATEVER YOU NEED"

    // Start process and wait for it to end
    Process exeProcess = Process.Start(startInfo)
    exeProcess.WaitForExit();

    // Display what the exit code was
    Console.WriteLine();
    Console.WriteLine("Process exit code: {0}", exeProcess.ExitCode);
}

Create a Robocopy-process like this:

string command = $"Robocopy {sourcePath} {targetPath} /MIR /w:1 /r:10";
var copyTask = new Process()
{
    StartInfo = new ProcessStartInfo()
    {
        FileName = "CMD.exe"
        Arguments = $"/c {command}", 
        UseShellExecute = false,
        RedirectStandardOutput = true, 
        CreateNoWindow = true
    }
};

Use it like this:

copyTask.Start()
string Output = copyTask.StandardOutput.ReadToEnd();
copyTask.WaitForExit();

Now you have the whole log in the "string output". This is useful if you want to find out WHAT went wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top