RetryCount := 0; REPEAT UseSFTP := DataIntSetup."FTP Protocol" = DataIntSetup."FTP Protocol"::SFTP; CLEAR(FTPManagement); FTPManagement.SetConnectionDetails(DataIntSetup."FTP IP Address",DataIntSetup."FTP User Name",DataIntSetup."FTP Password",DataIntSetup."Source No.",DataIntSetup."FTP Port Number"); //try to connect 5 times i := 0; WHILE (NOT FTPManagement.Connect(TRUE, UseSFTP, FALSE)) AND (i < 6) DO BEGIN i += 1; SLEEP(2000); CLEAR(FTPManagement); FTPManagement.SetConnectionDetails(DataIntSetup."FTP IP Address",DataIntSetup."FTP User Name",DataIntSetup."FTP Password", DataIntSetup.Code,DataIntSetup."FTP Port Number"); END; IF i = 6 THEN BEGIN DirectDispatchFTPErrorLog.RESET; EntryNo := 1; IF DirectDispatchFTPErrorLog.FINDLAST THEN EntryNo := DirectDispatchFTPErrorLog."Entry No" + 1; CLEAR(logyourerrorTable); //log errors if you whish here, etc logyourerrorTable.INSERT(TRUE); EXIT; END; IF FTPManagement.DownloadFiles(FTPPath,LocalPath,ArchivePath,FilesFound,UseSFTP,RemoveFilesAfterTransfer) THEN EXIT ELSE IF NOT FilesFound THEN //no files on server just exit EXIT ELSE BEGIN //problem occurred with transfer so re-try 3 times RetryCount += 1; SLEEP(1000); END; UNTIL RetryCount = 3;
FTPUser := _FTPUser; FTPPassword := _FTPPassword; FTPIP := _FTPIP; FTPPortNo := _FTPPortNo; CustNo := _CustNo;
IF GUIALLOWED THEN Window.OPEN('Interrogating FTP Server for File List to Transfer'); RemoteDirectoryInfo := FTPSession.ListDirectory(RemoteFolder); RemoteFileInfoCollection := RemoteDirectoryInfo.Files; IEnumerator := RemoteFileInfoCollection.GetEnumerator; WHILE IEnumerator.MoveNext DO BEGIN RemoteFileInfo := IEnumerator.Current; IF NOT RemoteFileInfo.IsDirectory THEN BEGIN vStrLen:=STRLEN(RemoteFileInfo.Name); vStrLen+=1; REPEAT vStrLen-=1; vChar:=COPYSTR(RemoteFileInfo.Name,vStrLen,1); UNTIL vChar='.'; vExtension:=UPPERCASE(COPYSTR(RemoteFileInfo.Name,vStrLen+1,STRLEN(RemoteFileInfo.Name))); IF vExtension IN ['EXE','COM','BAT','CMD','MSI','VB','VBS','WS','WSF','SCF','SCR','PIF', 'PDF','XLSX','XLSM','DOCX','DOC','XLS'] THEN ERROR(STRSUBSTNO(Error003,vExtension)); RemoteFilePath := CombineURL(RemoteFolder, RemoteFileInfo.Name); IF ArchiveFolder <> '' THEN RemoteArchivePath := CombineURL(ArchiveFolder, RemoteFileInfo.Name); IF NOT DownloadFile(RemoteFilePath, LocalFolder, UseSFTP) THEN MESSAGE(Error001, RemoteFilePath) ELSE BEGIN IF ArchiveFolder <> '' THEN MoveFTPFile(RemoteFilePath,RemoteArchivePath); IF RemoveFilesAfterTransfer THEN RemoveFTPFile(RemoteFilePath); END; END; END;
Answers
https://winscp.net/eng/download.php
I cant give you all the code but this should keep you going:
variables:
Name DataType Subtype Length
DirectDispatchFTPErrorLog Record Direct Dispatch FTP Error Log
FTPManagement Codeunit WinSCP FTP Management
setconnectiondetails function, just sets some globals:
download files function:
variables:
Name DataType Subtype Length
RemoteDirectoryInfo DotNet WinSCP.RemoteDirectoryInfo.'WinSCPnet, Version=1.7.2.10278, Culture=neutral,
RemoteFileInfoCollection DotNet WinSCP.RemoteFileInfoCollection.'WinSCPnet, Version=1.7.2.10278, Culture=neutral,
IEnumerator DotNet System.Collections.Generic.IEnumerator`1.'mscorlib, Version=4.0.0.0, Culture=neutral,
RemoteFileInfo DotNet WinSCP.RemoteFileInfo.'WinSCPnet, Version=1.7.2.10278, Culture=neutral,
Name DataType Subtype Length
IEnumerator DotNet System.Collections.Generic.IEnumerator`1.'mscorlib, Version=4.0.0.0, Culture=neutral,
https://rockwithnav.wordpress.com/2017/10/31/file-upload-sftp/
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/