Article
· Apr 23, 2019 3m read

FTP file download (all files, no of files) Option : copy file or move file

Method : to download the FTP file from intersystem cache : If you have any question please drop a message :

ClassMethod FTPDownload(myFTP = "", myUserName = "", myPassword = "", sFileLocation = "", dLocation = "", noOfdownloadFile = 1, sourceFileDel = 0)
{

 /*---------------------------------------------------------------------------------------------------------------------------
 download the file according to the requirements  : FTP

Method : Resuable 

by : Sanjib Raj Pandey on 30/03/2018

 downLoadFile = No of files or all files  ...... you want to download; DEFAULT VALUE IS 1
   = give value eg. 1,3,7,100 files
   = type "*"  to to download all the files.
 
  SourceFileDel = If you want to delete files from source folder after download..... 
                  then setup value 1  -- ;   the default value is :  0 
                  1= True (delete file from source folder after download),  0 = False (copy only)
 
  sFileLocation = Source of file location (folder)
  dLocation = Destination folder

Example :
  The following ..... move all files from source to destination location
  w ##class(CW.COMMON).FTPDownload("IP Address","User Name","Password","Source folder","Destination folder,"*",1)
 
The following...... copy 200 files from source to destination folder
w ##class(CW.COMMON).FTPDownload("IP Address","User Name","Password","Source folder","Destination folder,200,0)

  -------------------------------------------------------------------------------------------------------------------------
 */
 // you can setup the error control e.g. Try and Catch 
 
 Set (count,fileNo,key,messge,fileStream,myFileName,myFile,fSave,eMessage,eSubject)=""
 Set fIp= myFTP 
 Set fUserName= myUserName 
 set fPassword=myPassword 
 set sFileLocaion=sFileLocation 
 set dLocation=dLocation 
 Set downloadFile=noOfdownloadFile 
 Set sourceFileDel=sourceFileDel
 
 If $Length(fIp)=0||($L(fUserName)=0)||($L(fPassword)=0) || ($L(downloadFile)=0) "Invalid Credentials or Download file is 0.!, check IP, Username,           password or FTP location or destination location !"      
 Set myFtp=##class(%Net.FtpSession).%New()
 Set eMessage="FTP connection has failed, please check "_fIp_" or username, password.!"
 Set eSubject ="FTP Warning Message."  
 Set myFtp.Timeout = 60
 If 'myFtp.Connect(fIp,fUserName,fPassword) Quit  $$EVEMAIL^CW.COMMON(eSubject,eMessage)
 Do myFtp.SetDirectory(sFileLocaion)
 If 'myFtp.NameList(" ",.x) Quit "File(s) not found "
 Set fileStream = ##class(%Stream.FileBinary).%New()
 Set message ="copied"
 Set myFileName=""
 Set fileNo=0
 Set Key=""
 If (downloadFile = "*")
 {
   While (x.GetNext(.Key))'=""
 {
       Do StartCopy
 }
Do myFtp.Logout()
Quit fileNo_" file(S) has been "_message_" successfully !"   
 }
 
 If (downloadFile >0)
 {
       Set count=1
        While ((count <= downloadFile) && (count<=x.Count()))
  {
   do StartCopy
  Set count=count +1
  }
  Do myFtp.Logout()
  Quit fileNo_" file(S) has been "_message_" successfully !" 
 }
  
StartCopy
  Set myFileName= x.GetNext(.fileNo)
   Do myFtp.Binary()
  Do myFtp.Retrieve(myFileName,.fileStream)
  Set myFile= ##class(%Library.FileBinaryStream).%New()
   Set myFile.Filename=dLocation_myFileName
  Do myFile.CopyFrom(fileStream)
  Set fSave=myFile.%Save()
   IF ((sourceFileDel=1) && (fSave = 1))
  {
     Do myFtp.Delete(myFileName)
      Set message="moved"
}

Discussion (0)0
Log in or sign up to continue