WinSCP退出代码未反映在ERRORLEVEL变量中

时间:2015-04-03 19:04:10

标签: batch-file sftp winscp

不确定这个windows批处理和winscp命令中缺少什么,我很难看到ERRORLEVEL值除了0之外,即使在winscp命令返回错误代码大于0的负面情况下也是如此。

见下面的例子。

我提供了错误的用户名和密码,并期望winscp程序填充大于0的ERRORLEVEL。

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

和控制台日志是......

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...

如果上述结果是预期的,那么有什么方法可以识别连接建立失败的时间,因此脚本可以执行回退命令..

我在Windows 8操作系统中使用winscp 4.3。如果我在这里遗漏任何东西,请告诉我....

1 个答案:

答案 0 :(得分:-1)

您可以尝试正确编写程序。

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

如果错误时显示0,则程序不会设置退出代码。