Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

I believe the StdOut.ReadAll() alone blocks until the process finishes. So I think that if you just swap the lines as I have suggested, it should be fine.
PrgSRR

Hello Martin:

Thank you for your findings. And you are absolutely CORRECT...!!!!!! :D :)

The exit code is getting captured in this script, before Winscp.com could complete execution.
I just had to add the lines of code to make the script wait for the console session to complete and that resolved the issue.

Corrected code is as follows:

'********************************************
Set objShell = WScript.CreateObject("WScript.Shell")
Set objShellExecute = objShell.Exec(FTPCommand)
'********************************************
Do Until objShellExecute.Status = 1
WScript.Sleep 100
Loop
'********************************************
FTPSessionExitCode = objShellExecute.ExitCode
FTPSessionResult = objShellExecute.StdOut.ReadAll()
If FTPSessionExitCode = 0 Then
FTPSessionResult = FTPSessionResult & "Batch Script executed. FTP Session completed without error(s). Exit Code: " & FTPSessionExitCode
Else
FTPSessionResult = FTPSessionResult & "Error(s) occurred with batch script execution or within the FTP Session. Exit Code: " & FTPSessionExitCode
GlobalErrorLevel = 1
End If
Set objShellExecute = Nothing
Set objShell = Nothing
'********************************************

Thank you very much indeed, for looking into this issue for me. Wishing you all the best in your upcoming endaevours.

Cheers!
Sairam
martin

I think, you are reading exit code before WinSCP finishes.

Try this order:
FTPSessionResult = objShellExecute.StdOut.ReadAll()

FTPSessionExitCode = objShellExecute.ExitCode
PrgSRR

Thanks for the response.

I am using %comspec& /c just so I can run multiple commands. However, I have discarded the errorlevel statement now and I am just using one statement, which is the winscp /command statement.

I have enclosed the piece of vbscript code that runs this command and the output as well. Please check it at your earliest convenience and let me know what could be causing the exit code to come out as 0 always.

*********************************
VBScript Code:
*********************************
FTPCommand = "winscp /command "echo @@@@@@ Setting command options: " "option confirm off" "option batch abort" "option exclude */" "echo @@@@@@ Connecting to FTP server: " "open USERNAME:PASSWORD@FTP.SERVERNAME.COM" "echo @@@@@@ Setting local directory: " "lcd D:\FTPJobs_ScriptLibrary\TEST\Inbox\" "echo @@@@@@ Setting remote directory: " "cd Example/Test/" "echo @@@@@@ Setting file transfer mode: " "binary" "echo @@@@@@ Listing contents of remote directory: " "ls" "echo @@@@@@ Transferring file(s): " "get *.pgp -nopermissions -nopreservetime" "echo @@@@@@ Disconnecting from FTP server: " "bye" /log="WinSCP_SessionLog.txt""
Set objShell = WScript.CreateObject("WScript.Shell")
Set objShellExecute = objShell.Exec(FTPCommand)
FTPSessionExitCode = objShellExecute.ExitCode
FTPSessionResult = objShellExecute.StdOut.ReadAll()
If FTPSessionExitCode = 0 Then
FTPSessionResult = FTPSessionResult & "Batch Script executed. FTP Session completed without error(s). Exit Code: " & FTPSessionExitCode
Else
FTPSessionResult = FTPSessionResult & "Error(s) occurred with batch script execution or within the FTP Session. Exit Code: " & FTPSessionExitCode
GlobalErrorLevel = 1
End If
Set objShellExecute = Nothing
Set objShell = Nothing

*********************************
Script Output
*********************************

***********************************************
10/4/2013 9:40:01 AM | BEGIN: Script Log
***********************************************
10/4/2013 9:40:01 AM | Downloading file(s) from FTP Server to Temp Directory:
|@@@@@@ Setting command options:
|confirm off
|batch abort
|include |*/
|@@@@@@ Connecting to FTP server:
|Searching for host...
|Connecting to host...
|Authenticating...
|Using username "USERNAME".
|Authenticating with pre-entered password.
|Authenticated.
|Starting the session...
|Reading remote directory...
|Session started.
|Active session: [1] USERNAME@SERVERNAME.COM
|@@@@@@ Setting local directory:
|D:\FTPJobs_ScriptLibrary\TEST\Inbox
|@@@@@@ Setting remote directory:
|Error changing directory to 'Example/Test/'.
|Cannot get real path for '/Example/Test/'.
|No such file or directory.
|Error code: 2
|Error message from server (en): File not found
|Request code: 16
|/
|
Batch Script executed. FTP Session completed without error(s). Exit Code: 0
Step: DownloadFiles: Completed without error(s).
***********************************************
10/4/2013 9:40:01 AM | Checking Temp Directory:
No. of file(s) in the Temp Directory: 0
No input files for this file transaction.
***********************************************
10/4/2013 9:40:01 AM | END: Script Log
martin

First, in your batch file, separate the %comspec% /c winscp ... and echo ... %ERRORLEVEL% into two lines and show us the output.

Also, why do you run WinSCP via %comspec%?
PrgSRR

Thank you very much for the response.

Your explanation of the %errorlevel% variable is understandable. However, I am also capturing the exit code of the script by using Shell.Exec and Exec.ExitCode from the VBScript. Which also received a value of 0 which is what is confusing. Any help on this is very much appreciated.
martin

Re: WinSCP: Issues With Exit Code

This does not work, as the %ERRORLEVEL% is resolved before WinSCP is run.

Try %comspec% /c dir c:\nonexistingdirectory\ & echo errolevel = %ERRORLEVEL% . You will get:

The system cannot find the file specified.

errolevel = 0
PrgSRR

Re: WinSCP: Issues With Exit Code

Thank you for the response.

Here is the batch script that I am executing:

-------------------------------------------------
%comspec% /c winscp /command "echo @@@@@@ Setting command options: " "option confirm off" "option batch abort" "option exclude */" "echo @@@@@@ Connecting to FTP server: " "open USERNAME:PASSWORD@FTP.SERVERNAME.COM" "echo @@@@@@ Setting local directory: " "lcd D:\FTPJobs_ScriptLibrary\TEST\Inbox\" "echo @@@@@@ Setting remote directory: " "cd Example/Test/" "echo @@@@@@ Setting file transfer mode: " "binary" "echo @@@@@@ Listing contents of remote directory: " "ls" "echo @@@@@@ Transferring file(s): " "get *.pgp -nopermissions -nopreservetime" "echo @@@@@@ Disconnecting from FTP server: " "bye" /log="WinSCP_SessionLog.txt" & echo FTP Session (WinSCP) ERRORLEVEL: %ERRORLEVEL%
-------------------------------------------------

And as can be seen, I am capturing the session errorlevel in the last statement. It receives a value of 0 and I am also capturing the exit code from within the VBScript which runs this batch script (by using Shell.Exec and Exec.ExitCode) and that receives a value of 0 as well.

However, in the (attached) WinSCP logs, I see that the exit code which is actually returned is 1. Not sure what the problem is. Please let me know what could be causing this and how to correct this.

Thank you very much indeed, for the kind support.

Cheers!
Sairam
martin

Re: WinSCP: Issues With Exit Code

Please attach a full log file showing the problem (using the latest version of WinSCP).

To generate log file, use /log=path_to_log_file command-line argument. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you may email it to me. You will find my address (if you log in) in my forum profile. Please include link back to this topic in your email. Also note in this topic that you have emailed the log.
PrgSRR

WinSCP: Issues With Exit Code

Hello:

I am using winscp.com via command line.

I am preparing a batch script (winscp.com /command "command1" "command2" "etc.") from within VBScript and executing it (Shell.Exec) and trying to catch the exit code.

When there are errors (Ex: file not found, invalid command, etc.) within the session, the exit code returned stays as 0 regardless of how many errors are there.

NOTE: I have the batch mode set to: option batch abort which should return a non-zero exit code for errors, and still this issue occurs.

Please let me know what could be causing this and how to correct it.

Thank You.

Cheers!
Sairam