Using the %lasterror Variable
The %lasterror variable is initialized to the last error or completion code of the most recently completed script command. The corresponding error text is stored in the %lasterrormsg internal variable.
Some commands do not change the value of %lasterror when they complete successfully. These commands include DISPLAY, SET and conditional branching commands like IFERROR. This exception allows you to add robust error handling logic to your command scripts.
If you want to use this value to report an error via email it is necessary to save the value in a script file variable so that it doesn't get reset by the results of a subsequent command. In the following example, the value of %lasterror is stored in a variable named err_num for use later in the body of an email message.
IFERROR= $ERROR_SUCCESS GOTO next_step
SET err_num = %lasterror
SET err_msg = %lasterrormsg
SET from = "Robo-FTP Service"
SET email = "firstname.lastname@example.org"
SET subj = "Automated download failed! (do not reply)"
SET body = "The following error was reported: "
SET body = body + "[" + err_num + "] " + err_msg
SET body = body + %crlf + "The command that caused this error was: " + %lastfailedcmd
SET attach = ""
CREATEMAIL from email subj body attach
SENDMAIL "220.127.116.11" "Help Desk" "email@example.com"