· Nov 30, 2023

How to download files from FTP Server to local FileSystem

s ftp=##class(%Net.FtpSession).%New()

 s ok=ftp.Connect(server,user,password,port)
 s ftp.Timeout = 100
 i ok {
   s ok=ftp.SetDirectory(directory) 
   i ok {
     n files,file
     s ok=ftp.NameList(directory,.files)
     i ok {
       f i=1:1:files.Count() {
         s file=files.GetAt(i)
         i (ftp.SetDirectory(file)) {
           d ftp.SetToParentDirectory()
         s fname=$p(file,"/",$l(file,"/"))
         s ok=ftp.Retrieve(file,.data)

Product version: Caché 2018.1
Discussion (1)2
Log in or sign up to continue

Hello @Shashvati Dash 

After you retrieve, you can save the file creating a %Stream.FileBinary and change the FileName to your directory in local FileSystem,

Set tStream = ##class(%GlobalCharacterStream).%New()
$$$THROWONERROR(tSC, ftp.Retrieve(file,tStream))
Set tFile = ##class(%Stream.FileBinary).%New()
Set tFile.Filename = "D:\"_file
Do tFile.WriteLine(tStream.Read())
Do tFile.%Save()
Set tSC = ftp.Logout()