1. John Bailey
  2. PowerBuilder
  3. Friday, 8 June 2018 17:56 PM UTC

Replacing FTP with WINSCP to perform SFTP from my application, and I am having trouble decoding (cycling thru) the result set from
"ListDirectory". The winscp examples use syntax like --  (foreach $fileinfo in $files) ... great - doesn't work in PB.

I have all the other functions of winscp that I need working from within my PB App - I can connect to the server, upload/download,
determine file existence, and get file details for a specified file or folder. However List Directory returns a collection and I can't extract
the list of files. I can get the number of files/folders in the collection, but not file info in the collection.

My PB Code (lines 1-40 for demonstration work):

01 oleobject i_winscp_ftp
02 i_winscp_ftp = create oleobject
03 l_rc = i_winscp_ftp.ConnectToNewObject("WinSCP.Session")

04 oleobject i_SessionOptions
05 i_SessionOptions = create oleobject
06 l_rc = i_SessionOptions.ConnectToNewObject("WinSCP.SessionOptions")

07 oleobject o_FileInfo
08 o_FileInfo = create oleobject // "WinSCP.RemoteFileInfo"

09 i_sessionOptions.Protocol = 0 // use SFTP
10 i_SessionOptions.HostName = as_server
11 i_SessionOptions.UserName = as_user
12 i_SessionOptions.Password = as_pwd
13 i_SessionOptions.SshHostKeyFingerprint = as_server_finger

14   // CONNECT using session options ..
15 i_winscp_ftp.Open(i_SessionOptions)

16 // demo - get file info about a specified file/folder ..
17 boolean lb_file_exists
18 lb_file_exists = i_winscp_ftp.FileExists(as_filefldr)
19 if lb_file_exists then
20 // if specified file or folder exists, get info
21   o_FileInfo = i_winscp_ftp.GetFileInfo(ls_filefldr)

22   string ls_name
23   ls_name = o_FileInfo.FullName
    
24   long ln_len
25   ln_len = o_FileInfo.Length
    
26   DateTime ld_date
27   ld_date = o_FileInfo.LastWriteTime

28   character lc_type ; long ll_type // ascii of type character code
29   ll_type = o_FileInfo.FileType
30   lc_type = char(ll_type)
31     // case '-' = 'Regular File' ; case 'D' = 'Directory' ; case 'L' = 'Symbolic Link'
                
32    MessageBox('getfileinfo' , &
33     ls_name + '~r~n Len=' + string(ln_len) + ' date=' + string(ld_date) + ' type= ' + lc_type )
34
35 end if // -- end line 19 lb_file_exists
36
37 oleobject o_RemoteDirInfo  // Session.ListDirectory return class
38 o_RemoteDirInfo = create oleobject
39
40 try
41   o_remotedirinfo = i_winscp_ftp.ListDirectory(ls_remote_dir);
42   string ls_objname
43   ls_objname = o_remotedirinfo.tostring() // RemoteFileInfoCollection
44   long ll_filecount
45   ll_filecount = o_remotedirinfo.Files.Count; // this value correct
46   MessageBox('List Directory', ls_objname + '~r~n Knt1= ' + string(ll_filecount) )

//   can not get each file info class out of collection ..
//
// winscp example shows -- foreach (RemoteFileInfo fileInfo in directory.Files)
// How to get that in PowerBuilder ??
//
// -- ElementAt method listed in ICollection class definition.
// -- could be used to loop thru all records (have filecount),
// -- call compiles but does not run. Maybe syntax issue?

47   o_FileInfo = o_remotedirinfo.Files.ElementAt(0) // 0..n-1 -- ERRORS
48   MessageBox('List Direcotry File Info', o_FileInfo.ToString() )
49
50 catch (throwable exc1)
51     MessageBox("winscp client Try-Catch error", exc1.text)
52 end try

Any help appreciated.

Thanks.

 

Who is viewing this page
Alan Tepper Accepted Answer Pending Moderation
  1. Thursday, 27 April 2023 15:19 PM UTC
  2. PowerBuilder
  3. # 1

I realize I'm resurrecting an old thread here, but did anyone ever get this to work?  I'm coming up against the same issue, trying to loop through the results of List Directory.

Thanks in advance!

Comment
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Tuesday, 23 March 2021 19:29 PM UTC
  2. PowerBuilder
  3. # 2

ForEach is like For/Next in PowerBuilder accept that it figures out the max and current index for you.

If there is a method to return the number of occurrences, you can do a For/Next loop and use [ll_idx] to indicate the occurrence index.

 

Comment
  1. SANTHOSH S
  2. Wednesday, 24 March 2021 06:19 AM UTC
Thanks Roland.

Now we were unable to establish the connection between WINSCP and PowerBuilder application and it shows class name not found.



Could help me to fix this issue.
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Tuesday, 23 March 2021 15:20 PM UTC
  2. PowerBuilder
  3. # 3

Is your WinSCP 32 or 64 bits?

Comment
  1. SANTHOSH S
  2. Tuesday, 23 March 2021 16:36 PM UTC
Could you please suggest me a 32 bit version please.
  1. Helpful
  1. Miguel Leeuwe
  2. Tuesday, 23 March 2021 16:42 PM UTC
Santhosh,

Never mind, there is ONLY 32 bit version of WinSCP, so no way you had 64 bit installed.

From: https://winscp.net/eng/docs/requirements#client : "While there is 32-bit version of WinSCP available only, it runs on 64-bit editions of Microsoft Windows. "

So my initial idea that it might be due to having to install 32 bit is no good.

Have you tried the "square brackets" that Ronald suggested?

regards

  1. Helpful
  1. SANTHOSH S
  2. Tuesday, 23 March 2021 16:51 PM UTC
"Square brackets" cannot be used for establishing a connection with WINSCP application.

It shows array error.
  1. Helpful
There are no comments made yet.
SANTHOSH S Accepted Answer Pending Moderation
  1. Tuesday, 23 March 2021 10:36 AM UTC
  2. PowerBuilder
  3. # 4

Hi John,

 

Thanks for the example code 

I have installed the WINSCP in machine but  I am facing the error in line number 3 because the return code is -2 and I am using PB 2017 R3.

Could you please help me on this.

01 oleobject i_winscp_ftp
02 i_winscp_ftp = create oleobject
03 l_rc = i_winscp_ftp.ConnectToNewObject("WinSCP.Session")

 

Thanks in advance ..!!

Comment
  1. SANTHOSH S
  2. Tuesday, 23 March 2021 18:02 PM UTC
I am getting the below mentioned err while trying with CMD prompt ,

The module "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" was loaded but the entry point DllRegisterServer was not found

Make sure that "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" is a valid DLL or OCX file and then try again.
  1. Helpful
  1. SANTHOSH S
  2. Wednesday, 24 March 2021 06:27 AM UTC
HI John,



Whether the issue in your machine is fixed if yes please share the steps.

  1. Helpful
  1. Akshay Shinde
  2. Tuesday, 14 February 2023 13:53 PM UTC
Hi Santosh,



is your query resolved. if yes then please share the steps. i am facing same issue that you face.

same error message that you got at the time of register dll file.

  1. Helpful
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Friday, 8 June 2018 18:09 PM UTC
  2. PowerBuilder
  3. # 5

Try square brackets

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.