How to exchange files between SFTP and Nav 2009

I got an project that, we are using Navision 2009, we are about to exchange some files with SFTP, how should we do? Is there some sample coding I can refer to?

Best Answers

  • txerifftxeriff Member Posts: 480
    edited 2021-12-21 Answer ✓
    I think you can run dotnets in 2009, therefore I suggest to try winscp:

    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
    
    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;
    
    

    setconnectiondetails function, just sets some globals:
    FTPUser := _FTPUser;
    FTPPassword := _FTPPassword;
    FTPIP := _FTPIP;
    FTPPortNo := _FTPPortNo;
    CustNo := _CustNo;
    


    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,
    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

  • txerifftxeriff Member Posts: 480
    edited 2021-12-21 Answer ✓
    I think you can run dotnets in 2009, therefore I suggest to try winscp:

    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
    
    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;
    
    

    setconnectiondetails function, just sets some globals:
    FTPUser := _FTPUser;
    FTPPassword := _FTPPassword;
    FTPIP := _FTPIP;
    FTPPortNo := _FTPPortNo;
    CustNo := _CustNo;
    


    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,
    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;
    
    
    
    
    

  • rxqrxqrxq_zydrxqrxqrxq_zyd Member Posts: 16
    thanks finanlly we were using the Power shell to communicate with local folder and SFTP to process it, and it worked.
Sign In or Register to comment.