The second argument to dir returns an array of $lists. For example:
SERVER='ftp.somewhere.com'
FTP="%Net.SSH.Session"->%New()
st=FTP->Connect(SERVER)
if st#1 then stopm "connect failed: ":$SYSTEM.Status->GetErrorText(st)
st=FTP->AuthenticateWithUsername("user","password")
if st#1 then stopm "authentication failed: ":$SYSTEM.Status->GetErrorText(st)
st=FTP->OpenSFTP(SFTP)
if st#1 then stopm "open failed: ":$SYSTEM.Status->GetErrorText(st)
st=SFTP->Dir("/usr",%zFL)
if st#1 then stopm "dir request failed failed: ":$SYSTEM.Status->GetErrorText(st)
$xecute "zwrite %zFL"
SFTP->CloseSFTP()
outputs something like:
%zFL(0)=$lb("lib",4096,"D",16749,0,0,1378799188,1315336988)
%zFL(1)=$lb("libexec",4096,"D",16877,0,0,1378799188,1315413095)
%zFL(2)=$lb("share",4096,"D",16877,0,0,1368750176,1316192110)
%zFL(3)=$lb("..",4096,"D",16749,0,0,1379368850,1364647396)
%zFL(4)=$lb("lib64",36864,"D",16749,0,0,1378799189,1352536774)
%zFL(5)=$lb("src",4096,"D",16877,0,0,1368750177,1315336640)
%zFL(6)=$lb("sbin",12288,"D",16749,0,0,1378799187,1352536774)
%zFL(7)=$lb(".",4096,"D",16877,0,0,1379369820,1316099167)
%zFL(8)=$lb("local",4096,"D",16877,0,0,1368750177,1315336639)
%zFL(9)=$lb("games",4096,"D",16877,0,0,1378799187,1259933605)
%zFL(10)=$lb("bin",36864,"D",16749,0,0,1378799187,1328626987)
%zFL(11)=$lb("etc",4096,"D",16877,0,0,1368750177,1259933605)
%zFL(12)=$lb("oper",4096,"D",16877,0,0,1379304002,1379304002)
%zFL(13)=$lb("users.tar",255621120,"F",33188,0,0,1315927201,1315927339)
%zFL(14)=$lb("include",4096,"D",16877,0,0,1368750177,1315336960)
%zFL(15)=$lb("tmp",10,"L",41471,0,0,1379318406,1315336640)
%zFL(16)=$lb("users",4096,"D",16877,0,0,1378400950,1279118602)
I used a %variable for the argument so that I could pass it to $xecute and use ZWRITE to dump it with. Note also that I saved and tested the return values for all the method calls--there's
potentially a lot of information in the status results when things go wrong.
You get a complaint that FL is unassigned because the root node of the array isn't set. Often, arrays are returned with the root node set to the count of elements, but in this
case the index of the elements isn't a line count--it's the position count from the remote directory, and may not be sequential. To traverse this array you will need to use $order. For example:
dim FL()
st=SFTP->Dir("/usr",FL)
if st#1 then stopm "dir request failed failed: ":$SYSTEM.Status->GetErrorText(st)
idx=''
loop
idx=$order(FL(idx))
until idx='' do
crt idx,$list(FL(idx))
repeat
SFTP->CloseSFTP()
which outputs
0 lib
1 libexec
2 share
3 ..
4 lib64
5 src
6 sbin
7 .
8 local
9 games
10 bin
11 etc
12 oper
13 users.tar
14 include
15 tmp
16 users
to demonstrate what I meant above about the array indexes, add a filter to the Dir command: change
st=SFTP->Dir("/usr",FL)
to
st=SFTP->Dir("/usr",FL,"u*")
and the output is now:
13 users.tar
16 users
There were 2 array elements returned, but their indexes were not 1 and 2.