Page 1 of 1

Success or failure echo

Posted: 04 Apr 2024 08:51
by gussy81
OK so i have a script working for me. It mirrors one directory to the other and going forward it only mirrors files that have changed. I now want it to tell me if it has run successfully or not in the log as i want to run it out of hours daily:

Code: Select all

robocopy F:\ B:\ /e /mir /z /FFT /R:3 /W:5

Echo My Daily Backup Logged time = %time% %date%>>H:\log-of-daily-robocopy.txt
Thanks for any help

G

Re: Success or failure echo

Posted: 08 Apr 2024 08:34
by Sponge Belly
Hi Gussy! :)

If a command executes successfully, it sets errorLevel to 0, a non-zero integer otherwise. There’s an easy way to test for this and to take appropriate action. The syntax goes a little something like this:

Code: Select all

command && (
    success
    ) || (
        failure
        )
        
So, your code example would be rewritten as:

Code: Select all

2>nul robocopy F:\ B:\ /e /mir /z /FFT /R:3 /W:5 && (
    Echo(My Daily Backup Logged time = %time% %date%>>H:\log-of-daily-robocopy.txt
    ) || (
        Echo(Something went wrong with the robocopy backup!
        )
        
I hope this answers your question. If you have any follow-up questions, please feel free to ask.

- SB

Re: Success or failure echo

Posted: 08 Apr 2024 10:20
by Squashman
Robocopy has multiple return codes.
https://ss64.com/nt/robocopy-exit.html

Re: Success or failure echo

Posted: 08 Apr 2024 13:34
by Sponge Belly
Hi Squashman!

I didn’t know that about robocopy. Good to know, thanks! 8)

Gussy, don’t use the "command && success || failure" syntax with robocopy. Follow the example on the SS64 web page referenced by Squashman. Make sure you start with the highest errorLevel and work your way down to the lowest.

Good luck! :)

- SB