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:[email protected]" "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] [email protected]
|@@@@@@ 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: