Success notification on errorlevel 0

Advertisement

mariad
Guest

Success notification on errorlevel 0

I have a batch file that runs a winscp command. After the winscp command is run, i check for errorlevel - 0 means call 'PROCESS_SUCCESS' notification email, else call 'PROCESS_ERROR' notification email. I have noticed that whether the winscp command exited successfully or not, it always call the PROCESS_SUCCESS! Am I capturing the wrong ERRORLEVEL here for success/failure? I know it is failing because I am seeing a permission denied failure in the logs.

Here is part of the batch script running the winscp:
:WINSCP_SFTP_SCRIPT
for /f "tokens=*" %%z in (list.txt) do (
 cd "%soucedir%"
 set file=%%z
 winscp.com /console /script=script.txt /log=ftp.log /parameter %%z %sourcedir%
 if ERRORLEVEL 0 (
   call :PROCESS_SUCCESS
 ) else (
   call :PROCESS_ERROR
 )
)
pause
Here is what is in script.txt
option batch abort
option confirm off
open XXXX_XXXXXXXX@XXX.XXXX.com -hostkey="ssh-dss blah blah"
lcd "%2%"
put %1%
close
exit

Winscp 5.1.1 on Windows Server 2008 R2 Enterprise Service Pack 1 (64bit OS). SFTP Protocol.

I want to be able to send success/failure emails. For example, I got this error below but it seems like it is evaluating to ERRORLEVEL 0 no matter what!
< 2014-05-29 13:26:49.058 Status code: 3, Message: 1372932, Server: Permission denied, Language: en 
* 2014-05-29 13:26:49.058 (ETerminal) Permission denied.
* 2014-05-29 13:26:49.058 Error code: 3
* 2014-05-29 13:26:49.058 Error message from server (en): Permission denied
* 2014-05-29 13:26:49.058 Request code: 4
. 2014-05-29 13:26:49.059 Asking user:
. 2014-05-29 13:26:49.059 Cannot close remote file 'prdnssivr101_NEXTGEN_OSR_05282014.zip'. ("Permission denied.
. 2014-05-29 13:26:49.059 Error code: 3
. 2014-05-29 13:26:49.059 Error message from server (en): Permission denied
. 2014-05-29 13:26:49.059 Request code: 4")
< 2014-05-29 13:26:49.059 Script: Cannot close remote file 'prdnssivr101_NEXTGEN_OSR_05282014.zip'.
< 2014-05-29 13:26:49.060 Script: Permission denied.
< 2014-05-29 13:26:49.060 Error code: 3
< 2014-05-29 13:26:49.060 Error message from server (en): Permission denied
< 2014-05-29 13:26:49.060 Request code: 4
* 2014-05-29 13:26:49.061 (EScpSkipFile) Cannot close remote file 'prdnssivr101_NEXTGEN_OSR_05282014.zip'.
* 2014-05-29 13:26:49.061 Permission denied.
* 2014-05-29 13:26:49.061 Error code: 3
* 2014-05-29 13:26:49.061 Error message from server (en): Permission denied
* 2014-05-29 13:26:49.061 Request code: 4
. 2014-05-29 13:26:49.061 Script: Failed
. 2014-05-29 13:26:49.062 Script: Exit code: 1
. 2014-05-29 13:26:49.062 Closing connection.
. 2014-05-29 13:26:49.062 Sending special code: 12
. 2014-05-29 13:26:49.062 Sent EOF message

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

Re: success notification on errorlevel 0

The if ERRORLEVEL 0 means "if exit code is 0 or greater", what is always true.

That's why all the examples on this site and also the code that WinSCP GUI generates use the following syntax or similar:
if %ERRORLEVEL% equ 0
See https://winscp.net/eng/docs/guide_automation#results

Also read if /? help.

For a full example for sending emails, see https://winscp.net/eng/docs/script_email

Reply with quote

Advertisement

You can post new topics in this forum