%ERRORLEVEL% is always 0
We use
We have a batch file that calls
Now we wanted to extend the batch file to send an e-mail if a problem with the upload pops up (e.g. connection problems...).
However, we found that
Here is an excerpt from the batch file that calls WinSCP.com:
The script file contains nothing magical for our tests because we expect a connection problem anyway:
The log-file of the batch always shows 0 for
And that is from the WinSCP log file:
Why do we always get 0 in
Thanks for your support
WinSCP.com
(WinSCP-Portable V5.21.7) on a Windows Server 2019 Standard (64-bit OS, x64-based processor).
We have a batch file that calls
WinSCP.com
to upload specific files, if any, to an FTP server. That works so far.
Now we wanted to extend the batch file to send an e-mail if a problem with the upload pops up (e.g. connection problems...).
However, we found that
%ERRORLEVEL%
is always 0, regardless of whether the connection to FTP was successful or not.
Here is an excerpt from the batch file that calls WinSCP.com:
rem *** Set drive and directory for batch script execution... d: cd %HOME_DIR% echo. >> %LOG_FILE% echo ############################################# JOB START ############################################### >> %LOG_FILE% echo %nowDATE% %nowTIME% >> %LOG_FILE% echo ####################################################################################################### >> %LOG_FILE% if exist %SEARCH_FILES% ( echo ******************************************************************************************************* >> %LOG_FILE% echo Local files to upload found! Do upload to FTP server... >> %LOG_FILE% echo ******************************************************************************************************* >> %LOG_FILE% C:\Install\WinSCP-Portable\5.21.7\WinSCP.com /log=WinSCP_Log.log /ini=nul /script=FTP_ConnectionTest_Error.txt echo ERRORLEVEL %ERRORLEVEL% >> %LOG_FILE% if %ERRORLEVEL% equ 0 ( echo ******************************************************************************************************* >> %LOG_FILE% echo Move local files to the backup directory..." >> %LOG_FILE% echo ******************************************************************************************************* >> %LOG_FILE% move /Y %HOME_DIR%\%SEARCH_FILES% %BACKUP_DIR% >> %LOG_FILE% 2>&1 ) else goto JOB_ERROR ) else ( echo ------------------------------------------------------------------------------------------------------- >> %LOG_FILE% echo No local files to upload! >> %LOG_FILE% echo ------------------------------------------------------------------------------------------------------- >> %LOG_FILE% ) goto JOB_END :JOB_ERROR echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx >> %LOG_FILE% echo Error occurred! The job was canceled! Inform responsible users... >> %LOG_FILE% echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx >> %LOG_FILE% Powershell.exe -executionpolicy remotesigned -File SendWarningEMail.ps1 :JOB_END echo ############################################# PROGRAM END ############################################# >> %LOG_FILE% echo echo off
The log-file of the batch always shows 0 for
%ERRORLEVEL%
and moves the target file to the backup
directory even if a connection to the FTP could not be established and we actually expect something other than 0 for %ERRORLEVEL%
. Here what we see in this log file:
############################################# JOB START ############################################### 14.02.2023 14:26:44.46 ####################################################################################################### ******************************************************************************************************* Local files to upload found! Do upload to FTP server... ******************************************************************************************************* ERRORLEVEL 0 ******************************************************************************************************* ******************************************************************************************************* 1 file(s) moved. ############################################# PROGRAM END #############################################
... . 2023-02-14 14:26:44.713 -------------------------------------------------------------------------- < 2023-02-14 14:26:44.729 Script: Connecting to egFTP.com ... . 2023-02-14 14:26:44.729 Connecting to egFTP.com ... . 2023-02-14 14:26:54.756 Connection failed. < 2023-02-14 14:26:54.756 Script: Connection failed. < 2023-02-14 14:26:54.756 Connection failed.
%ERRORLEVEL%
, even if connection fails? What are we missing?
Thanks for your support