3

Not sure what is missing in this windows batch and winscp commands, I am struggling to see the ERRORLEVEL value apart from 0 even in negative scenario where winscp command returns errorcode greater than 0.

See the below example.

I have given incorrect username and password and expecting the winscp program to fill ERRORLEVEL greater than 0.

ECHO Connecting WinSCP...
SET TMPLOC_WINSCP="C:\Program Files\WinSCP\winscp.com"
set FILE_TO_GET=TEST.txt
%TMPLOC_WINSCP% /command ^
                "option batch abort" ^
                "option confirm off" ^
                "open ""DEVCONN""" ^
                "get %FILE_TO_GET%" ^
                "exit" 
ECHO ERRORLEVEL error code is... %ERRORLEVEL%
IF %ERRORLEVEL% NEQ 0 GOTO ERROR_WINSCP
echo File downloaded successfully... 

:ERROR_WINSCP
echo Error occured...%ERRORLEVEL%
GOTO END

and console log is...

Connecting WinSCP...
batch           abort
confirm         off
Searching for host...
Connecting to host...
Authenticating...
Using username "XYZ".
Authenticating with pre-entered password.
Access denied.
Connection has been unexpectedly closed. Server sent command exit status 0.
Authentication log (see session log for details):
Using username "XYZ".
Access denied.

Authentication failed.
ERRORLEVEL error code is... 0
File downloaded successfully...

If the above result is expected, then what are the ways to identify when a connection establishment has failed and accordingly script can execute fallback commands..

I am using using winscp 4.3 in windows 8 OS. Please let me know if i am missing anything here....

4
  • Your code looks good and works correctly for me. With exactly the same scenario (Access denied), I get to branch "Error occured". Both with the latest version (5.7.1) and your ancient 4.3. Please upgrade to the latest version, add /log=path\winscp.log to command-line and share the log with us. Commented Apr 3, 2015 at 19:48
  • I will provide the log from log file shortly. However i see that the windows script returns 0 on happy path flow sometimes and returns 1 sometimes to the same happy path flow. Is there something to do with ERRORLEVEL variable here and also should i consider delayed expansion (setlocal enabledelayedexpansion) as someone provided an answer stackoverflow.com/a/27923028/713414 Commented Apr 7, 2015 at 14:25
  • @MartinPrikryl It looks for me to be more of an issue with ERRORLEVEL internal variable. I see that WINSCP.com is setting the variable properly... Commented Apr 7, 2015 at 21:35
  • It is not related to delayed expansion, as there's no nesting in your batch file. Commented Apr 8, 2015 at 13:02

1 Answer 1

-1

You can try writing your program properly.

ECHO Connecting WinSCP...
"C:\Program Files\WinSCP\winscp.com" /command  "option batch abort" "option confirm off" "open ""DEVCONN""" "get TEST.txt" "exit" 
ECHO ERRORLEVEL error code is... %ERRORLEVEL%
IF ERRORLEVEL 0 If NOT ERRORLEVEL 1 echo Error 0
IF ERRORLEVEL 1 If NOT ERRORLEVEL 2 echo Error 1
IF ERRORLEVEL 2 If NOT ERRORLEVEL 3 echo Error 2
IF ERRORLEVEL 3 If NOT ERRORLEVEL 4 echo Error 3

If this shows 0 on error then your program doesn't set exit code.

Sign up to request clarification or add additional context in comments.

7 Comments

I like how you berate him for not writing his program properly and then proceed to write the if errorlevel statements backwards. If you put them in the order 3, 2, 1, 0, there's no need for the if not errorlevel parts. Alternatively, you can use %errorlevel% instead and then just use it like a normal variable.
My way is errorproof, yours isn't. This file was last updated 2007, and it says Test for errorlevel If errorlevel n if not errorlevel n+1 <command to do> As errorlevel tests always test for a number equal or higher, this code snippet only evaluates if the number is exactly the same. So if testing for errorlevel 6 If errorlevel 6 if not errorlevel 7 Echo The error level was 6 . Like with %errorlevel% they lead to BUGS.
You realize you can just say if %errorlevel% equ 0 and it will only return true if the error level is exactly 0, right?
Wrong. Very wrong. It will return true if errorlevel is 0 AND there is no configured environment variable called errorlevel. Once an errorlevel is created it is no longer will contain the errorlevel again. You can't control the environment, the user might type it, another batch file, or an install program. The user/batch file may do it as a bug rather than purposefuly. So for error proof code do it the way I said if the exact number matters (and again for error proof code you should test for a particular number not is it higher).
In retrospect, I've only seen errorlevel used with gotos, which is where the reverse ordering comes into play. I don't use them outside of choice, personally. I think in this circumstance, using the || operator would be a good idea instead.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.