1

I've been working on a batch file to copy files from my Google Drive to a local drive using Robocopy. I would like to monitor it for any errors so that I can then take actions (like sending an email to myself).

I have all parts working except for the "monitoring the error" part. Admittedly I probably don't understand error codes well enough. Below is a sample of my batch file (testing the error handling with simple echo statements). Currently I don't seem to get any feedback at all from the echo statements no matter what the errorlevel is. Nothing is ever printed out to the console.

robocopy %source% %dest% /e /copy:DATSO /purge /z /r:5 /w:5 /v /mon:1 /log+:%logfile% /rh:0700-1900
if %ERRORLEVEL% GTR 1 (echo You broke it) else (echo All is good)

What I would like to accomplish is to have Robocopy run for a portion of the day, monitor the source for changes, and, if there are any errors, notify me. I'm not married to doing it via the ERRORLEVEL, so if I'm missing a better option, I'd love to learn about it.

jxclark13
  • 31
  • 1
  • 6
  • 2
    Please read [ROBOCOPY Exit Codes](https://ss64.com/nt/robocopy-exit.html). `%SystemRoot%\System32\robocopy.exe` does not exit with just `0` on success and `1` on failure. It gives the users more information by using several exit codes. – Mofi Jan 29 '22 at 15:35
  • 1
    Hint: I read two days ago about the optional Windows update [January 25, 2022—KB5009616 (OS Build 17763.2510) Preview](https://support.microsoft.com/en-us/topic/january-25-2022-kb5009616-os-build-17763-2510-preview-d395eb9c-40e9-425c-a026-ed2e0a0d9dd3) for Windows 10, version 1809, Windows Server, version 1809, and Windows Server 2019 with the information: "Addresses an issue that prevents Robocopy from retrying the file copy process." So make sure the latest updates including optional updates are installed on using Windows 10 1809 or Windows server 2019. – Mofi Jan 29 '22 at 15:45
  • 1
    One more hint: Open a [command prompt](https://www.howtogeek.com/235101/), run `if /?` and read the output usage help. The described and recommended syntax to use is `IF [NOT] ERRORLEVEL number`. See also: [Single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564) for an explanation of the recommended syntax. – Mofi Jan 29 '22 at 15:47
  • @Mofi Thanks for the link to the exit codes. I've read that and other references to understand that Robocopy doesn't just give you pass/fail, but it's more nuanced. The specific error code isn't really my issue, it's that I don't know how to check what that code is while it's running in monitor mode. What I have in the batch file doesn't seem to do anything. – jxclark13 Jan 29 '22 at 15:48
  • You could use `if ERRORLEVEL 8 (echo A serious error occurred on copying the files!) else echo File copying was done successful.` – Mofi Jan 29 '22 at 15:53
  • Well, the usage of __ROBOCOPY__ in monitor mode with `/mon:1` and `/rh:0700-1900` results in `robocopy.exe` is never terminating itself. So the next command line is not reached ever as `robocopy.exe` is running 24 hours a day. There must be executed with a separate process `%SystemRoot%\System32\taskkill.exe /F /IM robocopy.exe` which results in the message *SUCCESS: The process "Robocopy.exe" with PID 4912 has been terminated.* (PID is variable) and `robocopy.exe` really terminates itself with exit code being always `1` in my test cases independent on files copied or not during monitoring. – Mofi Jan 30 '22 at 11:02
  • The usage of __TASKKILL__ as posted by me results in terminating all currently running `robocopy.exe` processes which is of course not good. It would be better to find out which process identifier (PID) the `robocopy.exe` process has which runs in monitor mode and kill just this process using option `/PID` with the correct process identifier instead of option `/IM robocopy.exe`. The option `/F` to force a kill should be in general not used, but must be used in this case as `robocopy.exe` in monitor mode does not terminate itself on using __TASKKILL__ without option `/F`. – Mofi Jan 30 '22 at 11:06
  • Another possibility to terminate __ROBOCOPY__ on being started during execution of the batch file is pressing __Ctrl+C__ with the console window having the input focus which was opened on starting `cmd.exe` to process the batch file and answer the prompt to terminate the batch job with NO. In this case `robocopy.exe` exits and `cmd.exe` continues processing the batch file. But the `ERRORLEVEL` value is in this case `-1073741510`. Conclusion: There is no chance to find out what __ROBOCOPY__ did while running in monitor mode with evaluation of `ERRORLEVEL`. – Mofi Jan 30 '22 at 11:10
  • There must be processed the last lines in the LOG file to find out if files/directories where copied and if there were one or more failures on copying files/directories on using __ROBOCOPY__ in monitor mode and terminate it somehow. – Mofi Jan 30 '22 at 11:12

0 Answers0