Success or failure echo

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gussy81
Posts: 3
Joined: 16 Jul 2019 08:56

Success or failure echo

#1 Post by gussy81 » 04 Apr 2024 08:51

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

Sponge Belly
Posts: 221
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: Success or failure echo

#2 Post by Sponge Belly » 08 Apr 2024 08:34

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

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: Success or failure echo

#3 Post by Squashman » 08 Apr 2024 10:20

Robocopy has multiple return codes.
https://ss64.com/nt/robocopy-exit.html

Sponge Belly
Posts: 221
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: Success or failure echo

#4 Post by Sponge Belly » 08 Apr 2024 13:34

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

Post Reply